$newRules = array(
'cpt-slug/(.+)/(.+)/(.+)/?$' => 'index.php?cpt-slug=$matches[3]',
'cpt-slug/(.+)/(.+)/?$' => 'index.php?cpt-slug=$matches[2]',
'cpt-slug/(.+)/?$' => 'index.php?cpt-slug=$matches[1]',
'cpt-slug/?$' => 'index.php?cpt-slug=$matches[0]',
'cpt-slug/(.+)/(.+)/(.+)/?$' => 'index.php?tax-slug=$matches[3]',
'cpt-slug/(.+)/(.+)/?$' => 'index.php?tax-slug=$matches[2]',
'cpt-slug/(.+)/?$' => 'index.php?tax-slug=$matches[1]',
'cpt-slug/?$' => 'index.php?tax-slug=$matches[0]',
);
]]>I have added some custom quer vars and those work fine so far:
function add_custom_query_vars($vars) {
$vars[] = "tdhm";
$vars[] = "tdht";
return $vars;
}
add_filter('query_vars', 'add_custom_query_vars');
Then I added a custom rewrite rule
function add_rewrite_rules($aRules) {
$aNewRules = array('tdh2015/danke/([^/]+)/([^/]+)/?$' => 'index.php?pagename=danke&tdhm=$matches[1]&tdht=$matches[2]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
And made sure, it was activated
$wp_rewrite->flush_rules( );
I then tested this rewrite rule with the monkeyman-rewrite-analyzer tool within wordpress and all looks fine so far. But when I try to access the variables within my custom page template, they never seem get populated.
actually the routing
“tdh2015/danke/myfirstparam/mysecondparam”
should translate to:
page: tdh2015/danke/
with the local variables
tdhm: myfirstparam
tdht: mysecondparam
available. I tried to access the variables via
if(isset($wp_query->query_vars['tdhm'])) {
$tdhm = urldecode($wp_query->query_vars['tdhm']);
}
if(isset($wp_query->query_vars['tdht'])) {
$tdht = urldecode($wp_query->query_vars['tdht']);
}
Has anyone here worked successfully with a similar setup?
Thanks in advance! Cheers, Matt
]]>What I’m trying to do is create a site that is targeting one of four different demographic types — students, parents, teachers and counsellors. Each one has a different presentation of the page(s) they are looking at — mostly different sidebars and possibly some different text on the page.
What I’ve got is a query variable that I’m adding to the url for each, so there’s a link on the home page for students to go to the /my-page/?dem=student version of that page and parents to go to the /my-page/?dem=parent version of the page. That’s all working.
Now I want to make /my-page/?dem=student turn into /student/my-page/. Which I’ve done using query_vars
and rewrite_rules_array
filters. Bingo. Now I can go to /student/my-page/ and I get my custom content that’s just for students.
What’s the problem?
So, the idea with this site is it’s a bunch of colleges and they’re presenting different information for the different groups and there’s like 30 or so different campuses. So instead of /my-page/ it’s really going to be like /campus23/ (or, to be more accurate /cityname/). Each campus is a custom post type post.
My problem is that everywhere I look at tutorials and examples of rewrite_rules_array
filters, it’s usually something like this:
add_filter( 'rewrite_rules_array', 'my_new_rewrite_rules' );
function my_new_rewrite_rules( $rules ) {
$new_rules = array(
'students/([^/]+)/?$' => 'index.php?pagename=my-page&dem=students',
'parents/([^/]+)/?$' => 'index.php?pagename=my-page&dem=parents',
'counselors/([^/]+)/?$' => 'index.php?pagename=my-page&dem=counsellors',
'teachers/([^/]+)/?$' => 'index.php?pagename=my-page&dem=teachers',
);
$rules = $new_rules + $rules;
return $rules;
}
…and my problem is that I don’t want it to be just pagename=my-page
, I want the rewrite rules to apply to any post or page that has the ?dem={querystring} variable attached to it. Because otherwise, that would mean creating a list of rewrite rules that applied to every single campus as well as any other pages that may be available to those groups.
There has to be a way to do this, right?
]]>I’m not sure why WordPress doesn’t support multiple taxonomies. When writing domain.com/?mytaxonomy=mytaxonomyvalue&anothertaxonomy=anothertaxonomyvalue it doesn’t redirect.
I’m wondering if anyone have a solution on how to make SEO-friendly permalinks for multiple taxonomies. What I need:
1) domain.com/?mytaxonomy=mytaxonomyvalue&anothertaxonomy=anothertaxonomyvalue should redirect to domain.com/mytaxonomy/mytaxonomyvalue/anothertaxonomy/anothertaxonomyvalue/ or something like that.
2) domain.com/mytaxonomy/mytaxonomyvalue/anothertaxonomy/anothertaxonomyvalue/ should be a result of the matching taxonomies.
What I’ve done so far is that I’ve read the documentation and I know that the solution for 1) involves the redirect_canonical filter as described in this post https://wp.tutsplus.com/tutorials/creative-coding/the-rewrite-api-post-types-taxonomies/.
And the solution for 2) involves adding new rewrite rules using the rewrite_rules_array filter.
But now I need some help on how to continue. Maby there is already a plugin that is doing this?
]]>function insert_rewrite_rules( $rules ) {
$newrules = array();
$page_name = 'mypage';
//basic rule
$newrules['(' . $page_name . ')$'] = 'index.php?pagename=$matches[1]';
//search by id
$newrules['(' . $page_name . ')/(\d+)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]&search=1';
//url with page included
$newrules[(mypage)(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*/([^.]+)/(\d+).html$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[3]&bathrooms=$matches[5]&query=$matches[6]&page=$matches[7]';
//url with no page
$newrules[(mypage)(/(\w+)-bedrooms)*(/(\w+)-bathrooms)*(/([^.]+))*$'] = 'index.php?pagename=$matches[1]&bedrooms=$matches[3]&bathrooms=$matches[5]&query=$matches[7]';
return $newrules + $rules;
}
add_filter( 'rewrite_rules_array','insert_rewrite_rules' );
The rules are well saved, this is not the problem.
But redirection is not working at all. I’m able to match the url without the page (https://mysite.com/2-bedrooms/3-bathrooms/mycity), but when I add the “/2.html” wp doesn’t understand it and redirect me to https://mypage.com/2/, then this url is interpreted applying the rule of “search by id”, mentioned above.
I recently moved to wp version 3.4.1, in version 2.9.2 it worked just fine.
All I need is a regex to match with the above given url, including the current page.
Please, give me some hints.
Thanks in advance
Things have veen working extremely well until I updated worpdress to 3.1. It seems that all of the custom rewrites for categories do not work now. We have a few filters for some pages and those still work.
Here is a sample code of what I’m using:
$newrules[‘(‘.$category_nicename.’)/(‘.$filter_name.’)-(‘.$filter_id.’)/?$’] = ‘index.php?category_name=$matches[1]&ss_filter=$matches[2]&filter_id=$matches[3]’;
So up until 3.1, everything worked. I am flushing the rules and adding the new variables to the query_var filter as well. See below
add_filter(‘rewrite_rules_array’,’ss_rewrite_rules_array’); Where I’m adding the new rules
add_filter(‘query_vars’,’ss_query_vars’); Adding the new vars (ss_filter and filter_id)
add_filter(‘admin_init’,’ss_flushRules’); Flushing rules when going to admin area.
I’ve searched around and seems a few more people are having the same issue.
Anyone with advise is greatly appreciated.
]]>add_filter(‘rewrite_rules_array’,’addCustomPage’);
function addBuscador($rules){
$newrules = array();$newrules[“mynewpage/([^/]+)?$”] = ‘index.php?p=13 keyword=$matches[1]’;
$rules = $newrules + $rules;
return $rules;
}
But when I go to my url https://MySite.com/mynewpage/somthing WordPress is redirecting to index.php?p=13 (Appears the address https://MySite.com/index.php?p=13 in my browser) and is not working as htaccess rewrite rule.
Could you help me? is possible that the function add_filter works as .htacess rewrite rule?
Thanks,
]]>add_filter(‘rewrite_rules_array’, ‘product_rules’);
add_filter(‘query_vars’, ‘add_query_vars’);
add_filter(‘init’, ‘flush_rules’);function flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}function product_rules($rules) {
$new_rules = array(
‘products/(\d+)-.+/?$’ => ‘index.php?pagename=products&product_id=$matches[1]’,
‘products/(.+)/(.+)/?$’ => ‘index.php?pagename=products&category=$matches[1]&subcategory=$matches[2]’,
‘products/(.+)/(.+)/(\d+)-(.+)/?$’ => ‘index.php?pagename=products&category=$matches[1]&subcategory=$matches[2]&product_id=$matches[3]’,
);
return $new_rules + $rules;
}function add_query_vars($vars) {
$vars[] = ‘product_id’;
$vars[] = ‘category’;
$vars[] = ‘subcategory’;return $vars;
}
i could use the 1 and 2 rules but on the 3rd rule
products/(.+)/(.+)/(\d+)-(.+)
it doesn’t work
the result is
$matches[1] = $category/$subcategory
$matches[2] = $product_id
which it should be
$matches[1] = $category
$matches[2] = $subcategory
$matches[3] = $product_id
I’ve been struggling with this for a while, and the documentation on the rewrite API is a little scarce. So I hope someone’s out there who can shed some light on this.
If I add rewrite rules, do I have to flush the rules on every page load? It seems that if I don’t flush them, it will keep working for a short while. Then after a couple of minutes it will break.
Flushing the rules currently causes an extra 100 (!) mysql queries on each page load. The queries look like this:
[23] => Array
(
[0] => SELECT * FROM wp_posts WHERE ID = 5106 LIMIT 1
[1] => 0.000135898590088
[2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post
)
[24] => Array
(
[0] => SELECT <code>post_parent</code> FROM wp_posts WHERE ID = 5068 LIMIT 1
[1] => 0.0001220703125
[2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index, get_page_uri, get_page, get_post, _get_post_ancestors
)
[25] => Array
(
[0] => SELECT ID, post_name, post_parent FROM wp_posts WHERE post_type = 'attachment' AND post_parent = 5106
[1] => 0.000179052352905
[2] => require, wp, WP->main, do_action_ref_array, call_user_func_array, XIC_flush, WP_Rewrite->flush_rules, WP_Rewrite->wp_rewrite_rules, WP_Rewrite->rewrite_rules, WP_Rewrite->page_rewrite_rules, WP_Rewrite->page_uri_index
)
They are “small” queries, but still I’d love to get rid of them..
]]>I’m implementing a site that has a ‘video blog’ section and an ‘article blog’ (i.e. non-video) section. I need to provide two index pages: one for videos and one for articles, each with a uri something like:
mysite.com/video
mysite.com/blog
and a static front page:
mysite.com/
I’m inclined to use a ‘video’ category to separate out video articles and filter these out from the main blog listing – say, using is_category(‘video’). That deals with the ‘article blog’.
For the ‘video blog’ section, I’m considering using the rewrite_rules_array filter to do a URI rewrite:
mysite.com/video ==> mysite.com/category/video
So, my questions:
Are there any pitfalls to this approach?
Are there more elegant solutions?
Is there a more maintainable solution (I could write an admin plug-in to provide flexibility in the video category naming).
I expect this problem has been solved many times before, though I’m struggling to think of the search terms to find them! Any pointers/links to previous solutions and related articles would be appreciated!!
Thanks,
Paul.
]]>