Extending the WordPress mod_rewrite rules

If you have written a WordPress Plug-in, or a highly customised page, and you want to add a custom rewrite rule then this is article is definitely the one for you.

We all know that WordPress is a powerful and pretty impressive blogging platform, and contains many useful features. The problem is finding the features you need / want. Most of the time they are available – somewhere.

In this case, after a numbers of hours research, I’ve done all the hard work for you when it comes to adding custom rewrite rules. We shall assume we have a page called “directory‘” with the page ID “123” and the parent page of name “business“.

This page (ID: 123) looks for the parameter “business” and if is defined, displays the details for the corresponding company. We also want to add the location of the business in the url, but for simplicity we will not pass them through in this example.

The end goal is to have a list of businesses displayed on “/business/directory/” with links to the business details in the format “/business/directory/york-yo31/mybusinessltd/“.

To achieve this, we will need to make use of three hooks. The first is the “init” hook, which we will use to regenerate the rewrite rules and save them to the database using flush_rules().

function directory_flush_rewrite()
{
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}
add_action('init', 'directory_flush_rewrite');

The second hook is “query_vars“. This is required to add the string “business” to the list of query variables WordPress understands in order for it to be retrieved on our directory page.

function directory_vars($public_query_vars)
{
    $public_query_vars[] = 'business';
    return $public_query_vars;
}
add_filter('query_vars', 'directory_vars');

The last hook is called “generate_rewrite_rules” and allows us to add the custom rewrite rule/s the list of WordPress rewrite rules. Putting the three code snippets together will result in the following block of code, which should be added to your “functions.php” file.

function directory_flush_rewrite()
{
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}
add_action('init', 'directory_flush_rewrite');
function directory_vars($public_query_vars)
{
    $public_query_vars[] = 'business';
    return $public_query_vars;
}
add_filter('query_vars', 'directory_vars');
function directory_rewrite_rules($wp_rewrite)
{
	$wp_rewrite->rules = array(
		'business/directory/(?:w+)-(?:w+)/(.*)/?' => 'index.php?page_id=123&business='.$wp_rewrite->preg_index(1)
	) + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'directory_rewrite_rules');

The final piece of information you need is how to access the variable passed through form the rewrite rule (“business=mybusinessltd”). This can be done using using the following:

$wp_query->get('business'); // returns 'mybusinessltd' in this example.

The example Reg Exp used could be modified and improved nad the page name could be matched and used instead of using the page id, but should put you on the right track. As mentioned earlier, you could also pass through the the other parameters (town & postcode) and use them alongside business in your lookup query.

Leave a Reply