ItecSoftware Logo

Custom URL Parameter Rewrites in WordPress

Written by Peter Gilg on - like this:
Custom URL Parameters

Recently, I was building a custom search page in WordPress for a gift idea site, where users could select the occasion (Birthday, Christmas …), the recipient (Women, Girl …) and the maximum price of the gift. In addition, I wanted the URL to be nice and SEO friendly, without query parameters.

Now, creating nice URLs within the confines of what WordPress has to offer out of the box is really simple by means of taxonomy and URL rewrites. But implementing a custom URL structure is a bit more daunting, or so it seemed at first. A bit of research and a solution was at hand, and actually quite simple to do URL Parameter Rewrites in WordPress.For this exercise, let’s assume we want to use the following URL:

www.example.com/gifts/christmas/girl/50

This entails variables for occasion, recipient and price. The non-frienly equivalent URL would look like this

www.example.com/gifts/?occasion=christmas&recipient=girl&price=50

Adding Rewrite Tags

First we need to declare the rewrite tags we’d like to use, to make WordPress aware of them. But careful, don’t choose tags that are already in use, otherwise they will get overwritten. These are declared using the add_rewrite_tag function and are best placed inside functions.php.

add_rewrite_tag('%occasion%','([^&]+)');
add_rewrite_tag('%recipient%','([^&]+)');
add_rewrite_tag('%price%','([^&]+)');

Behind the scenes, WordPress will use the non-friendly version and therefore we need the proper regex to filter them out, as in the code above.

Adding Rewrite Rules

Now that tags are in place, we need to add the rewrite rule that rewrites the friendly URL to non-friendly, so that WordPress can actually parse it and extract the values. For this we use the add_rewrite_rule function.

add_rewrite_rule('^gifts/([^/]*)/([^/]*)/([^/]*)/?','index.php?pagename=gifts&occasion=$matches[1]&recipient=$matches[2]&price=$matches[3]','top');

In the above case, we use a page called gifts, and pass our values which we then extract using the $matches variable. We also specify to add this rule at the top of the rewrite stack, so it’s handled first before other rewrites. After adding this code to functions.php, you will have to visit the permalink settings page (/wp-admin/options-permalink.php), this will flush the existing rewrite rule cache.

Extract URL Query Values

On the target page (/gifts), we can now extract the URL values like such.

$occasion = isset($wp_query->query_vars['occasion']) ? $wp_query->query_vars['occasion'] : false;
$recipient = isset($wp_query->query_vars['recipient']) ? $wp_query->query_vars['recipient'] : false;
$price = isset($wp_query->query_vars['price']) ? $wp_query->query_vars['price'] : false;

And that completes the implementation of custom URL parameter in WordPress. Any validation and security additions are ignored for simplicity.

Resources: add_rewrite_tag / add_rewrite_rule

Listed in Web Development

Tags: wordpress

3 responses to “Custom URL Parameter Rewrites in WordPress”

  1. Maritza says:

    Hello,

    Thanks for your post, I want to ask a question.
    I need to do something similar with search results. I have a form to search with one extra query var, now I see the URL like : “http://www.paginasamarillasdecundinamarca.com/?s=restaurantes&cat=11”. I want to have the url like: “http://www.paginasamarillasdecundinamarca.com/search/restaurantes/11”. I installed a plug in “Nice search” and it show the url = “http://www.paginasamarillasdecundinamarca.com/search/restaurantes”, but doesn’t include the category “11” in the results or in the URL. I tried to use your recommendation but it doesn’t work for me. Do you have any idea about what I can do? Or maybe I made someting wrong.
    Thanks a lot!!!!

  2. loi dufflot says:

    Sweet blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Many thanks

  3. Arun Verma says:

    Thanks for sharing this script.
    After searching lots of getting your code and it has worked as i want.

Leave a Reply

Your email address will not be published. Required fields are marked *