How to add custom rewrite rules to WordPress

If you have built your own custom WordPress plugin then you need to write custom rewrite rule to convert non-pretty URL to pretty URL or search engine friendly URL.  There are many ways to add your rewrite rules to update the default WordPress rewrite system, In this tutorial I will share a few examples on how to add custom rewrite rules to WordPress.

If you only want to add one rule, you can do this with add_rewrite_rule() function. If you want to learn more about add_rewrite_rule() then go to wordpress codex http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

wordpress rewrite rules

We are going to establish 2 rewrite rules in here for the following URLs:

1.example.com/index.php?pagename=portfolio&category_id=1&category_title=wordpress
2.example.com/index.php?pagename=portfolio-detail&portfolio_id=1&portfolio_title=this-is-title

which will be converted to pretty URL:

1.example.com/portfolio-lists/1/wordpress
2.example.com/portfolio-details/1/this-is-title

Where we have a portfolio page and we are going to show portfolio list depending on it’s category and title and we have another portfolio detail page where we want to show single portfolio details.

So STEP 1 is to add the rewrite rules to rewrite rules array filter hook. if you know more about this filter hook then go to WordPress codex http://codex.wordpress.org/Plugin_API/Filter_Reference/rewrite_rules_array

[php]function add_rewrite_rules($aRules)
{
$aNewRules = array(
‘portfolio-details/([^/]+)/([^/]+)/?$’ => ‘index.php?pagename=portfolio-detail&portfolio_id=$matches[1]&portfolio_title=$matches[2]’,
‘portfolio-lists/([^/]+)/([^/]+)/?$’ => ‘index.php?pagename=portfolio&category_id=$matches[1]&category_title=$matches[2]’,
‘top’
);
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);[/php]

In both cases we have tried to match two cases. If you want to add another condition for another URL  add ([^/]+)/ just before the closing ?$’ tag on left side and right site add 3rd case. Here you can see that we have added an extra URL ‘top’ at last. This is because we add it these rules on top of the list, otherwise, the “catch-all” rules at the bottom will win and your rules might not work.

Next STEP 2 is to keep the in the query variables so that you can use it later in wuery:

[php]function add_query_vars($aVars) {
$aVars[] = "portfolio_id";
$aVars[] = "portfolio_title";
$aVars[] = "category_id";
$aVars[] = "category_title";
return $aVars;
}
add_filter(‘query_vars’, ‘add_query_vars’);[/php]

We have successfully added all the WordPress custom rewrite rules. But if we want to access all these page in this stage then you will get 404 error page. That’s because we didn’t flush the rewrite rules.  You must tell wordpress that you have added some new rules. To do that go to Settings->Permalinks in your admin panel and flush(just click on the Save Changes button) the rules.

Here is the complete code. Just add this to your functions.php file

[php]
function add_rewrite_rules($aRules)
{
$aNewRules = array(
‘portfolio-details/([^/]+)/([^/]+)/?$’ => ‘index.php?pagename=portfolio-detail&portfolio_id=$matches[1]&portfolio_title=$matches[2]’,
‘portfolio/([^/]+)/([^/]+)/?$’ => ‘index.php?pagename=portfolio&category_id=$matches[1]&category_title=$matches[2]’,
‘top’
);
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);
function add_query_vars($aVars) {
$aVars[] = "portfolio_id";
$aVars[] = "portfolio_title";
$aVars[] = "category_id";
$aVars[] = "category_title";
return $aVars;
}
add_filter(‘query_vars’, ‘add_query_vars’);
[/php]

Please don’t forget to comment if like this. We appreciate your valuable comments.

Author: mytutorialguru

Share This Post On

Submit a Comment

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