My Tag Cloud

This plugin started as a request over at Kim’s blog. It turned out it was needed so here it is!


My tag cloud provides you with a widget that actually gives you the ability to list the tags of your posts in a diiferent way than a tag cloud. It list the first 5, most popular or alphabetically depending on the preferences, as an unordered list and the rest are put in a dropdown.



Download the plugin from here: [download#9#size]. Then place the file my_tag_cloud.php that can be found in the archive in your plugin directory. Activate it from the plugin management in your administration area. Finally, place the widget to your sidebar!


Setting the preferences

The default way the plugin works is this. It renders the 5 most used tags in an unordered list and the following are added on a dropdown. You can change this, along with the title, from the settings area within the code. Please open my_tag_cloud.php with your favorite text editor and notice the following code segment from line 18.

/*-------- PREFERENCES START --------*/
$list_tags = 5;//how many tags to show on the list before adding to the dropdown
$widget_title = 'My Tag Cloud';//title of the widget
$order_tags = 'count';//you can set this to "count" or "name".
					  //Count means the most popular will come first, name means they will be ordered alphabeticaly
/*-------- PREFERENCES END ----------*/

Note the following:

  • list_tags: This indicates how many tags should be listed on the unordered list.
  • widget_title: It’s obvious, this is the title the widget will have.
  • order_tags: This is the way you will order tags. You have two options: ‘count‘ or ‘name‘. If you choose ‘count‘ then the tags will be ordered from the most used to the least used. If you choose ‘name‘, they will be ordered alphabetically.

Shortening the tags

I got a very valid point as a valued feedback from a user here. As it seems the plugin actually broke his theme. The dropdown exceded the width of the widget so it looked bad. What happens is this. The dropdown’s length is defined from the bigger text in it. It seems he had “Search Engine Optimization” as a tag. You see it’s pretty big. To fix this you can trim your tags and add some “…” at the end. To do so please take a look at line 54. It says:

echo '<option value="'.$tags[$i]['name'].'">'.$tags[$i]['name'].' ('.$tags[$i]['count'].')</option>';

You need to remove that line and add the following:

$tag_length = 20;
if(strlen($tags[$i]['name']) > $tag_length){
	$tag_text = substr($tags[$i]['name'], 0, $tag_length).'...';
	$tag_text = $tags[$i]['name'];
echo '<option value="'.$tags[$i]['name'].'">'.$tag_text.' ('.$tags[$i]['count'].')</option>';

Please note that “20” which is the tag length is actually how many characters you want your tag not to exceed.