Get rewrite result

Have a look at the Action Reference to see generally how requests are processed. After all the loading and init stuff happens, the parse_request action runs when the request is matched against the rewrite rules, and an object is passed containing all the registered query vars and the matched rule and query string. function wpd_parse_request( … Read more

WordPress rewrite rules for state and city

Wasn’t as hard as I thought. Here’s the working example, in case anyone finds it helpful: function em_query_vars($vars) { array_push($vars, ‘state’); array_push($vars, ‘city’); return $vars; } add_filter(‘query_vars’,’em_query_vars’); function em_rewrite() { add_rewrite_rule( ‘^state/([^/]*)/([^/]*)/?’, ‘index.php?state=$matches[1]&city=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘^state/([^/]*)/?’, ‘index.php?state=$matches[1]’, ‘top’ ); add_rewrite_tag(‘%state%’,'([^&]+)’); add_rewrite_tag(‘%city%’,'([^&]+)’); } add_action(‘init’, ’em_rewrite’); function em_templates($template) { global $wp_query; if (isset($wp_query->query_vars[‘state’]) && isset($wp_query->query_vars[‘city’])) { … Read more

Change permalink for a single post entry

Try adding the following to your functions.php: function wpse221475_custom_rewrite_rules() { add_rewrite_rule( ‘^(special-project)?’, ‘index.php?post_type=projects&name=$matches[1]’, ‘top’ ); } add_action(‘init’, ‘wpse221475_custom_rewrite_rules’); Make sure to flush your rewrite rules after adding this rule. Visit: Dashboard -> Settings -> Permalinks Or if you wish, you can use flush_rewrite_rules() programmatically. To redirect requests for http://example.com/projects/special-project to http://example.com/special-project you can parse the … Read more

Change Query String to pretty permalink

Try this rewrite rule in your functions.php function string_url_rewrite() { global $wp_rewrite; add_rewrite_tag(‘%pet-page%’, ‘([^&]+)’); //In the rewrite rule you need to enter page slug and page id add_rewrite_rule(‘^Enter_Page_slug/([0-9]+)/?’, ‘index.php?page_id=Enter_Page_ID&pet-page=$matches[1]’, ‘top’); $wp_rewrite->flush_rules(true); } add_action(‘init’, ‘string_url_rewrite’, 10, 0); Now you also need to change the format of URL in anchor tag. For example, your previous URL http://www.example.com/my-account/view-pet/?pet-page=2 … Read more

htaccess: Remove trailing slash from URL ending with .xml/ only

If you’re outputting a sitemap, there’s no reason to wait for the query for your page- which is what is producing the redirect. Hook an earlier action and you won’t need anything to counter the trailing slash, because it won’t happen- EDIT Here’s a complete version with registering query vars, rules, and parse_request action: // … Read more

How to have multiple rewrite endpoints in the same URL?

I also need this to work: example.com/nice-page/my-gallery/animals/cats/my-lightbox/1234 Where I’d like to register my-lightbox as an additional endpoint or query_var, because it could be used independently from without my-gallery, such as: example.com/another-page/my-lightbox/2345 Here’s an example of doing it using add_rewrite_rule(): add_action( ‘init’, function() { // This will let you use get_query_var( ‘my-gallery’ ). add_rewrite_endpoint( ‘my-gallery’, EP_PAGES … Read more

Custom URL routing based on cookie value

SESSION variables aren’t reliable, and won’t work on some WP hosts. Even worse, you’re going to prevent caching But more importantly, don’t route, redirect! Routing and redirecting are not the same thing So, we want the following logic: on the template_redirect action if the user is on the homepage aka is_home call wp_safe_redirect redirect to … Read more