Implementing Tags for Movable Type

MT-Tagger 0.1

Abstract
A method of producing tags & tag pages from keywords using Movable Type, PHP, MySQL, and Mod_Rewrite.
MT-Tagger-0.1.zip

TOC

Dependencies
Movable Type using MySQL
Kellan Elliott-McCrea's MTKeywordList Plugin
Basic Knowledge of MySQL, PHP, & the like
Access to & basic knowledge of .htaccess

What this script does:
Dynamically create Tag pages for your entries

What this script does not do:
Right now I haven't figured out how to do a search for tags - this is pretty lame of me (I know)

Description
I think I'm not alone in my love for tags. I've written before about Folksonomies, and have wanted to figure out how to implement them on my site. So I sat down one night and wrote my own script to do it. You basically just enter tags into the Movable Type "Keywords" field separated by a space and then pull them dynamically out to render the Tag page.

The idea is pretty simple: you append any tag to the end of a URL (example.com/tags/TAG), and use a combination of Mod_Rewrite and a simple MySQL SELECT to show all entries with that tag. There are two items to this: the php file which does the tag search lifting (the tag cruncher) and formats the page, and Mod_Rewrite which gets rid of ugly URL's. I have built in the ability to use tags across multiple Movable Type blogs and the ability to choose which direction in time to order the results (ascending or descending in time).

The php script will pull out all of your keywords for a given (or multiple) weblog(s), check to see if there are any entries with that Tag, and then display the entries with that Tag. If there are no entries with a certain Tag, then there is a helpful page saying that there aren't any Tags present - this is editable inside the php file. You can build different file names depending on how your entries are saved. In my photo blog, my entries are saved as:

archives/YYYY/YYYYMMDD-dirified_file_name

I am using Adam Kalsey's Dirify php script to accomplish this (which is included in the source).

Caveats
Whoaaa, buddy - this is alpha software and it works for me, it might not work for you.

This is less than perfect, I know. If you are Tag Surfing for NYC and you ust enter in N then you get most of the results. There isn't a search function, partly because I'm lazy, and partly because I'm kind of stumped how to accomplish this. I was planning on using Mod_Rewrite and a FORM GET to append the Tag on the end (tagger.php=?TAG) and then have Mod_Rewrite get rid of the ugly parts so that the Tag Cruncher can do its' job. But I really haven't messed with it, and so far it hasn't really worked. So if you think you have a better idea, Email Me.

Instructions

  1. Download the code
  2. Decompress them on your computer
  3. Look through the three important files: index.php, .htaccess, & dirify.php
  4. Create a directory called "tags", so that your directory structure goes "example.com/tags/"
  5. Edit your the script for your system variables: BaseDirectory, DBName, DBHost, DBUser, DBPass, etc.
  6. Upload index.html, the supplied .htacces, & dirify.php to the "/tags/" directory
  7. Enjoy tags!

ToDo List:

  1. Add search capabilities
  2. Clean up code & make more extensible
  3. Decouple Tag Cruncher and display functions
  4. Add in hooks for most popular Tags
  5. Figure out Perl so I can make this into a Plugin

Code
Download the code, and see the three important files: index.php, .htaccess, & dirify.php & download Kellan Elliott-McCrea's MTKeywordList Plugin. Here's some code which will pull out the keywords for a given post and link them to your Tags page. You should put this in your Individual Archives Template:

<ul>
<MTKeyWordList><li><a href="http://example.com/tags/<$MTKeyWord$>" title="other entries tagged with <$MTKeyWord$>"><$MTKeyWord$></a></li></MTKeyWordList>
</ul>

I want to step back and go through some chunks of code which you might want to edit in the index.php file. Anywhere it says Example.com, change that to what your base URL is. Mine is whoarethey.org - yours will vary.

Then you need to format the output. As you can see from my tag page, I construct a image URL for thumbnails. You could easily change this to list all the entries instead of using thumbnails.

If you use Entry ID's, then you will have to make one change to the Display. Go to the code chunk which looks like this:


## uncomment below to use different values
# $day = $row['mtday'];
# $month = $row['mtmonth'];
$year = $row['mtyear'];
$date = $row['mtdate'];

## edit this line to display
echo "<li><a href=\"http://example.com/archives/$year/$date-$dirifyTitle\" title=\"$title\"><img src=\"http://example.com/archives/images/$date-thumb.jpg\" \></a></li>";
}

And change it to this:


## uncomment below to use different values
# $day = $row['mtday'];
# $month = $row['mtmonth'];
$year = $row['mtyear'];
$date = $row['mtdate'];
$entryID = $row['entry_id'];

## edit this line to display
echo "<li><a href=\"http://example.com/archives/$entryID\" title=\"$title\">$title</a></li>";
}

if you use the semi-standard Year/Month/Day/Dirified_Title method, you only need to make one change also to the Display code:


## uncomment below to use different values
$day = $row['mtday'];
$month = $row['mtmonth'];
$year = $row['mtyear'];
$date = $row['mtdate'];

## edit this line to display
echo "<li><a href=\"http://example.com/archives/$year/$month/$day-$dirifyTitle.html\" title=\"$title\">$title</a></li>";
}

That's about it! Look through the code and see where it is commented that you can change the display.

License
This is released under the The MIT License, © Copyright Randy Plemel 2005. Basically means that you can take my work and do whatever you want with it, as long as the notice is placed in the source:

Copyright (c) 2005 Randy Plemel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Trackback Pings

TrackBack URL for this entry,

Comments

Frankie Roberto says:

I went down a different route by using MT's built in multiple-category selector. This has the disadvantage of taking slightly longer to publish the entries (you have to save as draft first, use the pop-up window, and create any new categories) but it does have the advantages that you can easily display all entries for that tag, count number of entries which have that tag, and makes it fairly easy to create a Zeitgeist.

Posted by: Frankie Roberto at April 18, 2005 4:50 AM #

plemeljr says:

Frankie, that is pretty cool, but let me direct you to a (long) response, Categories vs Keywords in Movable Type.

Posted by: plemeljr at April 18, 2005 10:23 AM #

Post a comment




Remember Me?

(you may use HTML tags for style)

This is the permanent home of Implementing Tags for Movable Type. I wrote this post at 23:19 on April 16, 2005. This post is part of grubbykid.com, a weblog. If you liked this entry, why don't you read some other posts such as Long Island City Powerhouse or If I Were an Englishmen? Or you could go to the site archives or return home. All are good choices.

Remember this post with del.icio.us

Some descriptive tags for this entry are: mt code movabletype php mysql web tech.

Mommy... what's a tag?

Some descriptive tags for this entry are: code, movabletype, mt, mysql, php, tech, web.

Mommy... what's a tag?