We fear change
My beloved WordPress plugin, Ultimate Tag Warrior uses URL rewriting to make the good stuff happen. In the 2.0-RC1 release of WordPress, the way URL rewriting works has changed a great deal. What follows, is what I needed to do in order to get Ultimate Tag Warrior to work properly.
First: How Ultimate Tag Warrior worked previously
With old URL rewriting; a request for /tag/sometag
would be intercepted; and turned into index.php?tag=sometag
behind the scenes. Underlying PHP magic would populate the $_GET
array with the value of the tag parameter; then UTW would use the value from the $_GET
array to figure out taggy stuff.
Next: Why this doesn’t work anymore
Requests for /tag/sometag
are now matched in WordPress code; which means that the $_GET
array is no longer being populated with the name of the requested tag. This broke pretty URLs, since as far as the plugin was concerned, there wasn’t a tag being specified.
Finally: How to wrangle it so that it does work properly
There are three things that I needed to do to get UTW to play nicely.
Phase one: Make changes to my rewrite rules
Previously, my additions to the rewrite rules array looked like this:
$rules[substr($baseurl, 1) . "?(.*)$"] = "index.php?tag=$1";
I had to change them, to be like this:
$rules[substr($baseurl, 1) . "?(.*)$"] = "index.php?tag=$matches[1]";
The rewritten URLs changed from having references to $1…$n to having references to $matches[1]…$matches[n].
Phase two: Add my query variables to the list of public variables
I needed to add a new filter in order to get my little paws on the name of the current tag.
This is the filter. It just adds tag
to the list of query variables.
function ultimate_query_vars($vars) {
$vars[] = 'tag';
return $vars;
}
There’s a corresponding filter hookup. This is what mine looks like; because I have most (all?) of my actions inside a class named UltimateTagWarriorActions
.
add_filter('query_vars', array('UltimateTagWarriorActions','ultimate_query_vars'));
(For completeness; if you don’t stash your action functions in a class, it looks more like
add_filter('query_vars', 'ultimate_query_vars');
)
Phase three: Use the values from the query_vars
object instead of $_GET
This part of the exercise was relatively straightforward. I replaced references to $_GET["tag"]
with get_query_var("tag")
. Obviously, this gave me the name of the tag from the URL instead of an empty string (:
It was a pain to figure out the hoops that I needed to jump through to get this to work; so if you’re in the process of getting a pre WordPress 2.0 plugin to work, I hope that it helps.