Wordpress Basic Custom Rewrite Rule
I'm sorry fo开发者_如何转开发r this straightforward question that has doubtless been answered countless times on this site, but I can't figure out why my rewrite rule is not taking effect.
Here's my code:
add_action('init', 'update_rewrite_rules');
function update_rewrite_rules(){
add_rewrite_rule('^org/([^/]+)', 'index.php?pagename=about-us&org=$matches[1]', 'top');
add_rewrite_tag('%org%', '[0-9]+');
}
As far as I understand it, this should convert "myurl.com/org/13" to "myurl.com/index.php?pagename=about-su&org=13". What am I doing wrong???
I can write it into the .htaccess file just fine, but, of course, I run into the issue of Wordpress' custom PHP rewrite function superseding my code, so only the variable is found, but not the page.
Anyway, any advice or questions to think about would be appreciated! Thanks.
I believe you have to register your query variable before the actual rewrite rule. Also, as stated in Wordpress Rewrite API, it will only recognize the new rules once they are flushed and the cache is regenerated.
//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules();
Note that, for performance reasons, you should not do this every request, only on your plugin activation.
add_action('admin_init', 'flush_rewrite_rules');
There is also another way to add custom rewrite rules, as described in Custom Quesries.
精彩评论