How to add custom rewrite rule to .htaccess?

I found the solution on the Codex just after posting on the site and this is the code I was looking for: function my_htaccess_contents( $rules ) { $my_content = <<<EOD \n # BEGIN My Added Content # Protect wpconfig.php <Files wp-config.php> Order Allow,Deny Deny from all </Files> # END My Added Content\n EOD; return $my_content … Read more

Adding rewrite endpoint breaks static front page

Maybe I did not get this very well, but if you need to just remove ‘foo’ from query vars would not be much more simple to use the ‘request’ filter and remove the var from there? Code needed: add_filter(‘request’, function($query_vars) { return array_diff_key($query_vars, array(‘foo’=>”)); }); It: runs on main query only remove the var for … Read more

rewrite rules and querystring

You can add your own rewrite rule which will let you tweak the query via URL parameters: add_action( ‘init’, ‘rewrite_photo_url’ ); function rewrite_photo_url() { add_rewrite_rule( ‘photos/([^/]+)/?$’,’index.php?page=photos&photo_id=$matches[1]’, ‘top’ ); } If you need to use a custom variable, i.e. ‘photo_id’, you have to register the variable so it will be recognized by the query: add_filter( ‘query_vars’, … Read more

Passing parameters to a custom page template using clean urls

add_rewrite_rule() allows you to turn the pretty url into variables. numbers: (\d*) section: /rid/ or /pageid/ slug: ([a-zA-Z0-9-]+ Here is a class to register the rewrite and handle the request if a match has been found. <?php if ( ! class_exists( ‘CPTURLRewrite’ ) ): class CPTURLRewrite { const ENDPOINT_QUERY_NAME = ‘pageid’; const ENDPOINT_QUERY_PARAM = ‘__pageid’; … Read more

Dynamic Endpoints

10up engineering best practices as a great example on how to achieve that. You basically add a new rewrite tag and a new rewrite rule and then using the template_redirect action you return your custom action. Here is the code: add_action( ‘init’, function() { add_rewrite_tag( ‘%model%’, ‘([^/]+)’ ); add_rewrite_rule( ‘cars/([^/]+)/?’, ‘index.php?model=$matches[1]’, ‘top’ ); } ); … Read more

How to retrieve $_GET variables from rewritten URLs?

To be added on init: To register your custom variable (‘id’ in the question) add_rewrite_tag(‘%mycustomvar%’,'([^&]+)’); To create a re-write rule: add_rewrite_rule(‘^product/([0-9]{1,})/?’,’index.php?p=4&mycustomvar=$matches[1]’,’top’) 4 is the id of the ‘product’ page. You will need to flush rewrite rules once after adding these (go to Permalink settings page) You can get the value of mycustomvar: get_query_var( ‘mycustomvar’ ). … Read more

Using custom/dynamic “slug” for a page

Create a Page Template Add a new page and give it the slug stores Add A Public Query Variable add_filter(‘query_vars’, ‘add_state_var’, 0, 1); function add_state_var($vars){ $vars[] = ‘state’; return $vars; } Add a rewrite rule This will direct your request to your stores page. add_rewrite_rule(‘^stores/([^/]*)/?’,’index.php?post_type=page&name=stores&state=$matches[1]’,’top’); Within your Template You can access your state variable as … Read more