Wordpress Custom Post Type infinite redirect loop using Advanced Permalinks - wordpress

Using Wordpress 3.1 and the latest Advanced Permalinks and Custom Post Type UI i have created a custom post type called 'people'. The url pattern for all children of this post type are people/jim, however when i view post i get stuck in an infinite redirect loop. This only happens when I use pretty permalinks, not when id's are used.
The permalinks structure used on Advanced Permalinks are:
Common Settings
Custom Structure: %postname%
Post
%postname%
Wordpress is redirecting the custom post type to itself instead of translating it as ?people=jim.
I have tried defining the post types myself in functions.php and then doing a flush but that doesn't seem to fix the issue as others have found. Greatly appreciate any fix!

After a lot of logging and debugging, I found that the function that was causing the infinite redirect was function the_posts($posts). If you comment out everything from if (is_single () && count ($posts) > 0) to just before remove_filter ('the_posts', array (&$this, 'the_posts'));, it ceases the infinite redirect, and seems to still function fine! The only side-effect is that if you go to /jim/ it will redirect you back to /people/jim/ (rather than just giving you a 404). For me, that was Good Enough, since it fixed this problem.
So, again, within advanced-permalinks.php, search for function the_posts and then comment it so it looks like the code below.
Why does this break it? Because the custom post type is_single and doesn't match any rules, apparently, which makes APL send it back for re-direction... endlessly. Blah. Perhaps there is a more clever way to do this, to check if it is a custom post type, or whatever, but just disabling it seemed to do fine by me.
/**
* Hook that is called when a post is ready to be displayed. We check if the permalink that generated this post is the
* correct one. This prevents people accessing posts on other permalink structures. A 301 is issued back to the original post
*
* #return void
**/
function the_posts ($posts)
{
/* DISABLED CODE BELOW:
// Only validate the permalink on single-page posts
if (is_single () && count ($posts) > 0)
{
global $wp, $wp_rewrite;
$id = $posts[0]->ID; // Single page => only one post
// Is this a migrated rule?
$migrate = get_option ('advanced_permalinks_migration_rule');
if ($migrate)
{
if (isset ($migrate[$wp->matched_rule]) && substr (get_permalink ($id), strlen (get_bloginfo ('home'))) != $_SERVER['REQUEST_URI'])
{
wp_redirect (get_permalink ($id));
die ();
}
}
else
{
// Get the permalink for the post
$permalink = $this->get_full_permalink ($id);
// Generate rewrite rules for this permalink
$rules = $wp_rewrite->generate_rewrite_rules ($permalink);
// THIS IS ESPECIALLY PROBLEMATIC PART FOR CUSTOM POSTTYPES
// If the post's permalink structure is not in the rewrite rules then we redirect to the correct URL
if ($wp->matched_rule && !isset ($rules[$wp->matched_rule]))
{
wp_redirect ( get_permalink ($id));
die ();
}
}
}
DISABLED CODE ABOVE */
remove_filter ('the_posts', array (&$this, 'the_posts'));
return $posts;
}

Related

Wordpress Rewriting Rules

I have a Wordpress Multisite installation and I add custom post type named course in blog 1 and then I select the other blogs where I want see it.
In other blogs add a page that I use like single course template.
The url of this page is
http://example.com/blogName/course/?course-name=lorem-ipsum&course-id=xx
I would like to have a url like this
http://example.com/blogName/course/lorem-ipsum
and i would like to get the course-id parameter in template page.
I tried to use add_rewrite_rule but i'm not able to do what i want.
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'course-name';
return $vars;
}
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^course/([^/]*)/?','index.php?course-name=$matches[1]','top');
}
How can I do this?
I need to add something to .htaccess?
Once you've executed the above hooks, find a way to call the flush rewrite function which will update the rewrite cache so it should start working.
flush_rewrite_rules( true );
Documentation for this function can be found on the developer docs site.
You can indirectly call that function too by just saving the Permalinks settings in the dashboard by going to Settings > Permalinks.

Add specific words to WordPress custom type slug

I have a WordPress installation with a custom type (places) wich produces this permalink structure:
http://example.com/places/the-first-site/
And I would like to show all my sites like this:
http://example.com/places/visit-the-first-site-and-enjoy/
where 'visit' and 'and-enjoy' would be always those specific words (constants).
Even better I would like to put a custom taxonomy I have (year) as a metadata
places/visit-the-first-site-and-enjoy-1985/
I can access the DB and modify the post name of all post, but I would have to do for the new post also, and I'm looking for some automated rule, but can't find how to do.
Maybe some rewrite rule, but I don't know how to do it.
Any ideas??
You do not have to edit anything in your database nor any codes in your WordPress installation; you can achieve that directly from your WordPress administration panel.
From your Dashboard, click on the Permalink sub-menu under Settings; there, you will have to select the Custom Structure option and set it as follow in order to achieve your desired effect:
/places/visit-%postname%-and-enjoy-%year%/
Please note: here, we made use of both %postname% and %year% Structure Tags so as to get names of posts with their corresponding year of publication respectively.
Don't forget to click on the Save Changes button on the page in order to effect your changes.
... Read more on Permaklinks (for general knowledge).
For a custom post type and taxonomies as expressed further in comments, you will need a custom solution which will require a little bit of coding and or tweaking, depending on your abilities; you may use this handy plugin (Custom Post Type Permalinks) from the WordPress.org Plugins repository.
This posts should equally be of great help to you, to getting started and understanding further, should you chose to code.
You have différent hook like save_post or pending_to_publish, where you can set or change the "post_name" (that's corresponds to the permalink slug).
To Add specific words to WordPress custom type slug You have to Register custom rewrite rules.
suppose this is Your URL http://example.com/places/the-first-site !
and you have a post type places.
function add_rewrite_rules()
{
$wp_rewrite->add_rewrite_tag('%places%', '([^/]+)', 'places=');
$wp_rewrite->add_rewrite_tag('%state%', '([^/]+)', 'state=');
$wp_rewrite->add_permastruct('places', 'places/visit-%state%-and-enjoy/', false);
}
function permalinks($permalink, $post, $leavename)
{
$no_data = 'no-data';
$post_id = $post->ID;
if($post->post_type != 'story' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$state = get_post_meta($post_id, 'location', true);
if(!$state)
$state = $no_data;
$permalink = str_replace('%state%', $state, $permalink);
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);
put this code in your custom post type plugin. and if get page not found error please change your permalink setting. or make a custom template file to show this post.

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

modify url permalink wordpress

I use wordpress and I have the following question,
I use the URL:
- www.mysite.com/page
- www.mysite.com/cat/page
- www.mysite.com/custompostype/post
The value of permalinks is %category%/%postname%, everything works fine except now I have to use two languages ​​and I want to configure as follows:
www.mysite.com/EN/page
www.mysite.com/ES/cat/page
www.mysite.com/BR/custompostype/post
I want to change the structure of wordpress urls where the first rule is the language and to take it as variable value. Similar to /%lang%/%category%/%postname%/
i used the wp_rewrite and have had no success, any idea?
for now I just found a solution to my problem, which I handled sessions
<?php function init_sessions() {
if (!session_id()) {
session_start();
} } add_action('init', 'init_sessions'); ?> <?php function detecta_idioma() { if($_SESSION["IDIOMA"]["actual"]=="") {
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; } if($_GET["action"]=="idioma" &&
$_GET["lang"]) { unset($_SESSION["IDIOMA"]);
switch($_GET["lang"]) { case "EN":
$_SESSION["IDIOMA"]["actual"]="EN";
$_SESSION["IDIOMA"]["abreviatura"]="_en";
$_SESSION["IDIOMA"]["nombre"]="Ingles";
$_SESSION["IDIOMA"]["tag"]="EN"; break; case "ES": default:
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; break;
} } $IDIOMA=array("actual"=>$_SESSION["IDIOMA"]["actual"],"ab"=>$_SESSION["IDIOMA"]["abreviatura"],"nombre"=>$_SESSION["IDIOMA"]["nombre"],"tag"=>$_SESSION["IDIOMA"]["tag"]);
global $wp_rewrite;
$wp_rewrite->set_permalink_structure($_SESSION["IDIOMA"]["tag"].'/%category%/%postname%');
return $IDIOMA; } add_action('init', 'detecta_idioma'); ?>
works well on the route entries, eg www.misitio.com/en/post-demo, t not working with pages or categories or taxonomies, eg: www.misite.com/es/category/, www.site.com/es/page/subpage or www.site.com/br/taxonomy <----- Dont work, shows error 404 page
I can not use the plugin qTranslate and CMS plugin multulengual either they do not work well with the use of plugins Advanced custom fields and custom post types UI.
Thanks

Wordpress Plug-ins: How-to add custom URL Handles

I'm trying to write a Wordpress Plug-in but can't seem to figure out how you would modify how a URL gets handled, so for example: any requests made for:
<url>/?myplugin=<pageID>
will get handled by a function in my plug-in. I'm sure this is a very simple to do, but I'm pretty new to working with Wordpress and couldn't find it in the documentation.
In order to handle just a specific URL use the code below:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if(isset($_GET['myplugin']) && $_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if( isset($_GET['myplugin']) ) {
// do something
exit();
}
}
That should set you on the right direction. parse_request happens before WordPress runs any of the complicated WordPress queries used to get the posts for the current URL.

Resources