How to Fix Tags in Elgg

Update: My changes have been rolled into the current development version of Elgg, so they should be in the next release. So if you don’t want to implement these changes yourself, you should be ok waiting for the upgrade.

Tags are a really good way to organize content in Elgg. You can add tags to any piece of content you create, and the tags help you organize that content by grouping it together, however you see fit. But when I looked at the underlying code for the tags, I noticed that it wasn’t following the specification for the tag microformat. Not that big a problem, but not a hard problem to solve either.

To explain, here is an example of how Elgg currently formats tags.

<a href="http://www.example.com/search/?tag=elgg">Elgg</a>

According to the microformat specification, here is how tags should be formatted.

<a href="http://www.example.com/tag/elgg" rel="tag">Elgg</a>

So how do we correctly format tags in Elgg? Well, we can use an .htaccess rewrite rule to correctly format the url to the specification. Edit your .htaccess file in Elgg and insert this code at line #120.

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9+-]+)/?$ search/?$1=$2

That handles the url, but we need to rewrite the code in Elgg that displays the tags, so when you click on the tag, it goes to that url. To do this, open views/default/output/tags.php and edit line #49 to read like this.

$tagstr .= "<a rel="tag" href="{$vars['url']}tag/".urlencode($tag) . "{$type}{$subtype}{$object}">{$tag}</a>";

That should be it. It’s pretty simple actually. Now your tags should be correctly formatted to the tag microformat specification.

One important thing to note is that I have again modified core Elgg files, which means if I upgrade my install these changes will be overwritten. So if you follow these instructions, you should realize that. Yes, this is a no no, so I have again submitted a change to the Elgg core files in the bug tracker here. Hopefully, the developers will incorporate my code, or something like it so modifying the core files will no longer be needed. Or, if someone would write a plugin or create some decent plugin documentation on how to create something like this, it would also be appreciated.

I hate continually modifying the core Elgg files, but these are problems more mature software packages, like WordPress, have already solved. Yes, I believe Elgg is still in it’s infancy, but it’s fixes like this that will make people notice that this is serious software.