Alter The WordPress Tag Cloud Widget Output

WordPress provides a handy little widget where you can add your sites tags to a widgetised area and then are shown a tag cloud type format. However the output of this tag cloud cannot be controlled, like it can with the wp_tag_cloud() function. Having wanted to change the output I set about trying to achieve this. In this post I will outlined how to alter the output of the tag cloud widget in WordPress.

tag-cloud

The key to altering such things as this is to follow the WordPress golden rule which is never to edit the core files. Therefore to overcome this problem we need to look at the core files themselves for code which will allow us to manipulate the output of the widget without actually altering the core code.

Looking in the wp-includes folder of WordPress core we see a file named default-widgets.php. It is in this file where the Tag Cloud widget code is sourced. Looking at the code we can see that WordPress applies a filter to the arguments it supplies to the wp_tag_cloud() function. This filter is named widget_tag_cloud_args.

Therefore we can use this to manipulate the output of the tag cloud before it is outputted to the browser. The example below allows us (for example) to set the max and min font size for tags outputted.

[wpmark_gist id=”06c3402bd55e6288b9ea”]

This is a good example of WordPress’ extensibility, something which I will be speaking about at WordCamp Manchester at the end of June.

Leave a Reply

Your email address will not be published. Required fields are marked *