I’m using a custom page template that is intended to create a dynamic content using the wpdb class and some external tables within the database. I’m trying to keep the url’s structure as clean as possible, hence im using the ‘post name’ option in the permalinks..
I’ll just give the whole flow so it won’t be confusing.
- An user is trying to reach www.domain.com/dynamic/347
- The template is searching for ‘347’ in the database and creates a special dedicated page
- All the rendering is being executed within the template itself (the one that the /dynamic page is using)
The problem is with the mod_rewrite – Since I’m using the post_name option in wp’s permalinks – the wordpress itself is trying to access an actual page called ‘347’ under the parent of ‘dynamic’, instead of rendering dynamic’s template and use 347 as a parameter, by ‘exploding’ the url and retrieve the last value as the dynamic parameter, like this –
$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $dynamic_para = explode("/", $str, 4); // in this case - 347 //... retrieve dynamic_para from db and stuff with it.
How can I bypass wp’s current mod_rewrite specifically for this page? Is it even possible? I am trying to avoid sending GET variables with the url, e.g. www.domain.com/dynamic/?para=347
Advertisement
Answer
want is /dynamic/xxx will be rewritten to /dynamic?para=xxx
in WP you should rewrite to index.php with querystring (user still sees “/dynamic/xxx” in their browser address bar)
where exactly did you place the tc_query_vars_filter and the tc_rewrite_rules($rules) functions
You can place these in the (child) theme’s functions.php or better create add them to your own simple plugin so they will work with any theme:
e.g. create, upload, and activate:
<?php /* Plugin Name: Theme independent functions Description: NOT TESTED */ // allow WP to store querystring attribs for use in our pages function your_query_vars_filter($vars) { $vars[] = 'para'; return $vars; } add_filter( 'query_vars', 'your_query_vars_filter' ); // "rewrite" /dynamic/xxx to index.php?pagename=dynamic¶=xxx function your_rewrite_rules($rules) { global $wp_rewrite; $your_rule = array( // (not tested) 'dynamic/(.+)/?' => 'index.php?pagename=dynamic¶=$matches[1]' ); return array_merge($your_rule, $rules); } add_filter('page_rewrite_rules', 'your_rewrite_rules'); // any other site customisation stuff you want to add to this plugin ?>
N.B. above filter is for Pages; for POSTS use post_rewrite_rules filter instead
Then in your custom page template:
$my_search_var = get_query_var('para'); // sanitize as reqd
WordPress “caches” its re-write rules so finally we need to flush them so your new ones will be recognised:
The usual advice is you can flush by simply clicking “Save Changes” button on admin dashboard Permalink page (Settings->Permalinks) However, (either due to a quirk on my site or a change in the latest version of WP?) last time I did this I actually had to edit the permalink, save the change then change it back to what it should be, and save again for my rewrite functions rules to be applied.