Visually Test Your WordPress Rewrite URL Rules

Visually Test Your WordPress Rewrite URL Rules

WordPress URL RewritingWith functions like add_rewrite_rule(), WordPress allows you to create your own URL rewrite rules via the WP Rewrite API, of the WP_Rewrite Class.

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:

  1. Visit your Permalinks (just visiting the page is supposed to flush rewrite rules)
  2. Re-save your Permalinks (just do it to make sure all the flushing really worked)
  3. 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’.”

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

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):

Monkeyman Rewrite Analyzer plugin - screenshot 1
Monkeyman Rewrite Analyzer plugin – screenshot 1 (from plugin’s wordpress.org page)
Monkeyman Rewrite Analyzer plugin - screenshot 2
Monkeyman Rewrite Analyzer plugin – screenshot 2 (from plugin’s wordpress.org page)
WordPress Rewrite Rule Tester plugin - Monkeyman Help Info
Plugin contextual Help info
WordPress Rewrite Rule Tester plugin - Monkeyman Expanded Help Info
Plugin contextual Help expanded info

 

Compared to writing the rules, using this plugin is quick and easy. Enjoy!

Download this plugin from WordPress.org

Image: macro pixels url cliche from Cubosh