If you’re not sick of the word “rewrite” yet and that gobbley-gook sentence interests you, read on… :-)
Adding WordPress Custom Rewrite Rules
I can’t help you write your custom rewrite rules, but I can point you in the write direction (get it?) to make sure they work as desired.
Once you have your rewrite rules in you plugin or your theme’s functions.php, you then need to:
- Visit your Permalinks (just visiting the page is supposed to flush rewrite rules)
- Re-save your Permalinks (just do it to make sure all the flushing really worked)
- Test if your rewrite condition worked.
Steps 1 and 2 are just part of WordPress core. Step 3 is what we’re focusing on.
Manually Testing WordPress Custom Rewrite Rules
Most likely, you have a custom post type, a custom query variable, are creating an industry-specific theme, or are doing something custom where you say to yourself, “If someone visits this URL in their browser address bar, I want something else to be displayed” (i.e. rewritten).
You have a specific something in mind and you can test your rewrite rules by creating the page(s), post(s), attachment(s), adding that variable, etc. Then type in the pretty URL version you want to work (read more about ugly URLs in WordPress Query Overview) and hope it displays as desired.
Automatically Testing WordPress Custom Rewrite Rules
Adding custom rewrites isn’t the easiest thing so once you get it to work for the test scenarios you create, you’ll likely stop and be glad it’s over. However, your custom rewrite rules may match more than you expected and could result in some unintended consequences (e.g. rewrites that you didn’t intend to perform), which could cause less than ideal front-end displays or even 404 errors.
That’s where the Monkeyman Rewrite Analyzer plugin comes to the rescue!
This plugin, once activated, shows up under the WordPress Tools menu as “Rewrite analyzer”.
It lists all of your site’s rewrite rules in their order of priority (higher priority on top). As the plugin’s contextual “Help” menu (screenshot below) states, “The first match in the list ‘wins’.”
You can also type in a URL to see which, if any, rewrite rules are matched and in what priority.
Monkeyman Rewrite Analyzer Plugin Screenshots
Here are some screenshots of the plugin’s admin page (click for full size):
Compared to writing the rules, using this plugin is quick and easy. Enjoy!