This plugin isn’t intended to revolutionize the world, but it does solve a problem that some of you may have. BWB-ReWriter is a small helper plugin to let you easily add parameters to your “pretty urls” in your WordPress app. A big thanks to Gamerz and Zoom4 in this WP support topic for their info on how to make this work.
Download the plugin: BWB-ReWriter
What BWB-ReWriter does
For starters, this is version 0.1. In my case, that means that it works, it gets the job done, but you’ve got to open the code to add your own url’s.
BWB-ReWriter takes care of the necessary steps to let you add “pretty” parameters to your WordPress urls. This may be particularly needed if you’re using WordPress as a CMS. You may ask, “Why can’t I just add my rewrite rules to the .htaccess file?” The short answer is that WordPress obliterates them, as far as I can tell, and I may be wrong…I’m running 2.5.1. So, you have to do it the WP way. Here are the steps that BWB-ReWrite takes to make that happen:
- Flush rewrite rules so WP will recalculate them and pick up your new rules
- Add your custom rules to the $wp_rewrite->rules array. It adds your rules to the top. Mod Rewrite takes rules in sequence, so if a standard WordPress rule matches first, your rules will never be testing/applied.
- Add your parameters to the $public_query_vars[] array.
Add Your Custom Mod ReWrite Rules to the WordPress rules engine
WordPress has an object, $WP_Rewrite, that gets instantiated as $wp_rewrite by WordPress, and is free for you to use. There appear to be different ways of interacting with that object to customize your urls. So, the code below is probably just one way to get ‘r done. Here is the salient code from the plugin, along with the comments that walk you through what you need to change to add your urls:
/* Add your custom rules in the array below...the first part (the key,
to left of =>) is the regular expression to match, the second part
is the new value
*/
function bwb_add_rewrite_rules( $wp_rewrite )
{
/* Unsurprisingly by its name, this array contains your new rules.
The array uses your Regular Expressions (the expressions test the raw
URL for matches) as the keys. The values (the string to the right
of the "=>") are the new URLs. In the function below, you will add
your parameter names to $public_query_vars[].
Separate Key + Value pairs with a comma. Do not put a comma after
the last pair...that always gets me b/c I copy/paste a lot.
Change the page (regions) to your page name, change/add/remove
variables and their corresponding regex matches "(.+)" to match
your needs
*/
$new_rules = array(
'regions/(.*)/(.*)/(.*)' => 'index.php?page_id=4 ®ion='.
$wp_rewrite->preg_index(1).'&country='.
$wp_rewrite->preg_index(2).'&state='.
$wp_rewrite->preg_index(3),
'travel/(.*)/(.*)/(.*)' => 'index.php?page_id=4&country='.
$wp_rewrite->preg_index(1).' ®ion='.
$wp_rewrite->preg_index(2).
'&state='.$wp_rewrite->preg_index(3)
);
//Add the rules to the rules array..wanna add them to the TOP, like so
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function bwb_query_vars($public_query_vars) {
/* ADD YOUR PARAMETERS or QUERY VARIABLES BELOW
Uncomment the lines and change the variable names to the ones
you want to use. Add more lines as needed
*/
$public_query_vars[] = "region";
$public_query_vars[] = "country";
$public_query_vars[] = "state";
/* Note: you do not want to add a variable multiple times. As in
the example above, multiple rules can use the same variables
*/
return $public_query_vars;
}
Using the Query Variables in your Code
I almost forgot to tell you…now that you’ve got the URLs working, to get the query variable values, all you need to do is something like this:
$region = get_query_var(‘region’);
$country = get_query_var(‘country’);
$state = get_query_var(‘state’);
You’re all set.
Next Steps
Clearly, there is room for improvement here. I would like to add an options page that lets you type in your Match expressions, resultant values, and your variable names, and the plugin would pull these out of the database on the fly. That should work, and I intend on trying it. But here’s to version 0.1
That’s about it. Hope it helps somebody out there!
Cheers,
Byron



84 Responses to “WordPress Plugin: BWB-ReWriter for Custom URL Rewriting”
Trackbacks/Pingbacks