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.
Wednesday 5 April, 2006 @ 5:23 pm
[...] In my environment – WordPress 2.0.2 and Ultimate Tag Warrior 3.1 – friendly URL rewrites were not working (e.g. /tag/TAGNAME vs. index.php?tag=TAGNAME). Apparently WordPress 2 added a PHP rewrite layer for new plugin flexibility, but this rewriting occasionaly breaks traditional .htaccess rewrites. For instance, WordPress now internally handles “/tag/TAGNAME” requests. [...]
Sunday 7 May, 2006 @ 9:16 am
[...] A hack from Nathan’s blog to get friendly URLs (E.G.: http://www.kgadams.net/tags/) to work. This still sort of bothers me, since the UTW developer, Christine, seems to imply she has it working with WordPress’ internal URL rewrite features [...]
Tuesday 8 August, 2006 @ 9:34 pm
[...] The truth is, I spent most of the day researching just how to manipulate the rewrite rules. Needless to say, the official documentation was of little help in this matter. Sure, they’ve got a whole bunch of filters and hooks to get at the rewrite rules, but with little to no documentation on the vast majority of them, I can’t say I found much help there (however, I do realize that it’s a community-driven documentation effort, and that I have no place to complain unless I’m willing to do something about it myself). I found some discussion on the subject in a few places, but a step-by-step rundown of the required steps was not be found. As such, I spent most of the day feeling like I was going in circles, all the while missing the “key” to tying it all together. [...]