WordPress 2.0-RC1, Plugins and URL Rewriting

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.

Ultimate Tag Warrior 2.5

Ultimate tag warrior 2.5 is out! however, UTW now has it’s very own home at http://www.neato.co.nz/ultimate-tag-warrior where the juicy details will be posted for this, and other versions.

[Brief overview: 2.5 has big changes to the admin side of things; allows tag clouds; will render a long-tail graph for your tags; ought to behave in not-in-the-siteroot blogs; will clean up duplicate tags; can turn categories into tags; and anything else that I’ve forgotten]

I’ll continue to make announcements in the main part of my blog, though (:

Ultimate Tag Warrior 1.1

I’ve just released Ultimate Tag Warrior 1.1 into the wild! From here on (and indeed since last week), the latest version of Ultimate Tag Warrior is going to live here: http://dev.wp-plugins.org/browser/ultimate-tag-warrior/trunk/. I’ll keep on announcing new versions of it here, though.

Version 1.1 adds ultimate_show_related_tags(), which will display the tags related to the current tag set. If you are viewing /tag/monkey, it will list all of the tags of the posts that match monkey (except for monkey itself.) If you are viewing /tag/monkey+banana it will list all of the tags of the posts that match both monkey and banana (excluding the monkey and banana tags). Clicking the + beside a tag will add it to the tag set.

Dan made me write this suggested this feature.

Ultimate Tag Warrior 1.0

I’m so excited.. Ultimate Tag Warrior 1.0 is now available for download.

Changes
– /tags/Tag1+Tag2 syntax is now supported.
– The ultimate_show_post_tags now defaults to the local tag page; and allows specifying a set of external tag places to link to.

Functions
There are four functions worth thinking about (the rest is “don’t mind the man behind the curtain” stuff)

ultimate_show_post_tags($separator=” ”, $baseurl=’/tag/’, $notagmessage = “No Tags”, $morelinks=””)
Display a list of tags associated with the current post.
$seperator is the string that seperates tags. It only appears between tags, and not at the beginning or end of the list.
$baseurl is the base URL to link the text of a tag to. The tag link takes the form {baseurl}{tagname}.
$notagmessage is the text to display when a post has no tags.
$morelinks is either a single link as a string, or an array of links. The links are base urls to other systems that support tagging. As with the base url, the final links take the form of {url}{tagname}. Each link is displayed as an » for brevitys sake.
ultimate_show_popular_tags($limit=10)
Display a list of the most popular tags. The tags are wrapped in li html tags, and the number of matching posts is also displayed.
$limit is the maximum number of tags to display.
ultimate_tag_archive($limit = 20, $postlimit=20)
The tag version of this plugin
ultimate_tag_cloud($order=’tag’, $direction=’asc’)
A tag cloud viewer that lists the tags using the order and direction. $order can be either “tag” or “count”, $direction can be “asc” or “desc”. The list of tags is just displayed with spaces in-between, linking through to the tag page. CSS classes .taglevel1 -> .taglevel7 control the look of tags based on popularity.

Plugin Administration
Click on “Manage”, then “Tags”. This will display a list of all available tags. Clicking on the “Rename Tag” link will allow renaming the tag. Clicking on the “Delete Tag” link will allow deleting a tag (and the associations to posts).
You can rename a tag to be the same as another one if you really want to – the tag cloud and tag archive will merge the tags, as if by magic; but if a post is tagged with both versions, it will seem to be tagged twice with the same tag. Adding a thing to merge tags properly is on my todo list.

Download
http://www.neato.co.nz/plugins/ultimate-tag-warrior-1.0.zip

This file contains the full documentation, the plugin file, an installation script, and a basic tag.php template.

It’s Broken!
(I hope not.)

If it is, send me an email… christine at neato dot co dot nz (I.. uh.. still haven’t figured out the problem with comments… they work okay with the default theme, but there’s nothing I can see that’s different around the comment thing. Oh the irony.. I make this genius plugin, and I can’t get my base install to work properly.)

Ultimate Tag Warrior 0.2

Ultimate Tag Warrior 0.2 has been unleashed!

Changes
– I’ve added a tag cloud function.
– There’s an administration backend that allows editing the name of tags, and removing tags.
– I’ve fixed a couple of bugs, too (;

Functions
There are four functions worth thinking about (the rest is “don’t mind the man behind the curtain” stuff)

ultimate_show_post_tags($separator=” ”, $baseurl=’http://www.technorati.com/tag/’, $notagmessage = “No Tags”)
Display a list of tags associated with the current post.
$seperator is the string that seperates tags. It only appears between tags, and not at the beginning or end of the list.
$baseurl is the base URL to link the text of a tag to. The tag link takes the form {baseurl}{tagname}. The default points to Technorati. The value “/tag/” would link to the local tag page.
$notagmessage is the text to display when a post has no tags.
ultimate_show_popular_tags($limit=10)
Display a list of the most popular tags. The tags are wrapped in li html tags, and the number of matching posts is also displayed.
$limit is the maximum number of tags to display.
ultimate_tag_archive($limit = 20, $postlimit=20)
The tag version of this plugin
ultimate_tag_cloud($order=’tag’, $direction=’asc’)
A tag cloud viewer that lists the tags using the order and direction. $order can be either “tag” or “count”, $direction can be “asc” or “desc”. The list of tags is just displayed with spaces in-between, linking through to the tag page. CSS classes .taglevel1 -> .taglevel7 control the look of tags based on popularity.

Plugin Administration
Click on “Manage”, then “Tags”. This will display a list of all available tags. Clicking on the “Rename Tag” link will allow renaming the tag. Clicking on the “Delete Tag” link will allow deleting a tag (and the associations to posts).
You can rename a tag to be the same as another one if you really want to – the tag cloud and tag archive will merge the tags, as if by magic; but if a post is tagged with both versions, it will seem to be tagged twice with the same tag. Adding a thing to merge tags properly is on my todo list.

Download
http://www.neato.co.nz/plugins/ultimate-tag-warrior-0.2.zip

This file contains the full(ish) documentation (I, uh.. may have neglected to document the admin side), the plugin file, an installation script, and a basic tag.php template.

It’s Broken!
(I hope not.)

If it is, send me an email… christine at neato dot co dot nz.

Ultimate Tag Warrior 0.1

Version 1.0 is here!

This is the first release of my latest and greatest wordpress plugin Ultimate Tag Warrior. It’s a tagging plugin for wordpress with normalised data (Oooh. Aaaah); and three ways of getting tags back out (Tags on a post, popular tags, and an archive a-la my Category Archive plugin).

Functions
There are three functions worth thinking about (the rest is “don’t mind the man behind the curtain” stuff)

ultimate_show_post_tags($separator=” ”, $baseurl=’http://www.technorati.com/tag/’, $notagmessage = “No Tags”)
Display a list of tags associated with the current post.
$seperator is the string that seperates tags. It only appears between tags, and not at the beginning or end of the list.
$baseurl is the base URL to link the text of a tag to. The tag link takes the form {baseurl}{tagname}. The default points to Technorati. The value “/tag/” would link to the local tag page.
$notagmessage is the text to display when a post has no tags.
ultimate_show_popular_tags($limit=10)
Display a list of the most popular tags. The tags are wrapped in li html tags, and the number of matching posts is also displayed.
$limit is the maximum number of tags to display.
ultimate_tag_archive($limit = 20, $postlimit=20)
The tag version of this plugin

Download
http://www.neato.co.nz/plugins/ultimate-tag-warrior-0.1.zip

This file contains the full documentation, the plugin file, an installation script, and a basic tag.php template.

Category Archive plugin 0.2

Behold, version 0.2 of my category archive plugin!

Changes

  • Some type checking on the parameters passed in to the functions.
  • The addition of get_limited_category_archive(), which gets only the categories specified
  • The inclusion of a $getchildren parameter to the standard archive-getter, which allows excluding the display of child categories

Function Reference

get_category_archive($limit = 10, $parent = 0, $getchildren=true)
  • $limit is the maximum number of posts to get for a category. 0, -1, or none will get all of the posts.
  • $parent is the id of the parent category to get.
  • $getchildren specifies whether the children of the categories should be displayed
get_limited_category_archive($categories, $limit = 10)
  • $categories is the list of categories to retrieve. This can be either an array of category ids, or it can be a comma seperated list (for example: 1,2,5,7)
  • $limit is the maximum number of posts to get for each category. 0, -1 or none will get all posts.

Output

css class = categoryarchive

css class = categoryarchivename
{the name of the category}
css class = categoryarchivedesc
{the description of the category, if set}
css class = categoryarchiveposts
{the most recent postings for this category. If there are more postings than the limit specified, a link is included to the category page.}

Download
The plugin lives at http://www.neato.co.nz/plugins/category_archive.txt