Going from UTW to WordPress 2.3’s built in tag system was mostly painless. But there are somethings I miss about UTW. Two of those things are being able to display “Tags: None” and having my tags show up in my RSS feeds. Here is how I was able to put both back into my blog.
Adding “Tags: None” to my theme
I’m not a PHP programmer. I had thought I could just do $my_tags=the_tags(); and put in an if..then loop to check if it’s empty. That did not work; referencing the_tags() would cause the output to go to the $content and get displayed.
PHP has a function called ob_start(). From the manual page:
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
That’s good! Looking around some more I saw that using ob_get_clean() can put that buffer into my variable. That’s what I wanted so I could just capture and test the output. Here is the code I put into my theme template.
<?php if (function_exists(‘the_tags’)) {
ob_start();
the_tags();
$my_tags = ob_get_clean();
if ( $my_tags != “” ) {
echo $my_tags;
} else {
echo “Tags: None”;
}
} ?>
This lets me check the output from the tags and if it is empty then I can display “Tags: None” when I want.
Update: There is an easier way without using ob_start(), using get_the_tags().
<?php if (function_exists(‘the_tags’)) {
$my_tags = get_the_tags();
if ( $my_tags != “” ) {
the_tags();
} else {
echo “Tags: None”;
}
} ?>
I thought about this because a) it’s easier, and b) I think that wp-cache might do this and then my theme might cause issues.
Adding tags to my feeds
Making feed include tags was a simple matter as well. I had read on Angsuman’s blog about a plugin that lets you append copyright notices to your feeds. I downloaded it and took a look at the plugin. Simplest code in the world, I just modified Angsuman’s plugin. Here is what it looks like.
<?php
/*
Plugin Name: Add Tags to Feeds
Plugin URI:
Description: This adds WP 2.3 tags to your feeds.
Author: Jan Dembowski
Author URI:
Version: 1.0
*/
function addTagsToFeed($content) {
global $my_tags;
ob_start();
the_tags();
$my_tags = ob_get_clean();
if(is_feed()) {
return $content.”<p class=”tags”>”.$my_tags.”</p>”;
} else {
return $content;
}
}
add_filter(‘the_content’, ‘addTagsToFeed’);
?>
Nothing to it. My feed validates and now the tags are in the feeds too. Seeing how existing code uses add_filter is probably the best way to learn by example.