Rewrite rules in WordPress

Rewrite rules in WordPress

So it came to create a WordPress plugin. No meter about the functionality. I needed to add some control of incoming queries. So using rewrite rules was necessary. According to the documentation, it’s pretty simple.

add_rewrite_rule($regex, $redirect, $after);

So the example looks like this:

function custom_rewrite_basic() {
  add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');

And everything seems to work great except for that doesn’t work! No meter how I modified the regular expression, it didn’t even fart the query to my script. And then I found this sentence:

IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

Conclusion

Always read the documentation, especially the IMPORTANT sections. In fact simply open the Settings -> Permalinks and the rewrite rules will flush automatically without clicking Save Changes.

Leave a comment

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