Advanced Filter for Tag Cloud Widget Css Classes

tag cloud widget colored with css
tag cloud widget advanced color styling

The default tag cloud widget does not offer a lot of formatting possibilities, partly because it only outputs one css class with a reference to the tag ID.
It uses an inline style, which outputs the font-size, to distinguish the tags according the number of post they belong to.

This filter function is coded to add a css class with the tag slug .tag-slug-{slug} and with a rounded tag size .tag-size-{number}, and to remove the inline style.

add_filter ( 'wp_tag_cloud', 'tag_cloud_widget_advanced_classes' );  

function tag_cloud_widget_advanced_classes( $taglinks ) { 
	$tags = explode('</a>', $taglinks);
	$regex1 = "#(.*tag-link[-])(.*)(' title.*)#e";         
	$regex2 = "#(.*style='font-size:)(.*)((pt|px|em|pc|%);'.*)#e"; 
	$regex3 = "#(style='font-size:)(.*)((pt|px|em|pc|%);')#e";         
	$regex4 = "#(.*class=')(.*)(' title.*)#e";         
	foreach( $tags as $tag ) {         
		$tag = preg_replace($regex1, "('$1$2 tag-slug-'.get_tag($2)->slug.'$3')", $tag ); //add .tag-slug-{slug} class   
		$size = preg_replace($regex2, "(''.round($2).'')", $tag ); //get the rounded font size       
		$tag = preg_replace($regex3, "('')", $tag ); //remove the inline font-size style
		$tag = preg_replace($regex4, "('$1tag-size-'.($size).' $2$3')", $tag ); //add .tag-size-{nr} class
		$tagn[] = $tag;
	}     
	$taglinks = implode('</a>', $tagn);     
return $taglinks; 
}

For the code, also see the pastebin.

The added css classes can for instance be used to color the tags according to the number of appearance; as you can see here in the tag cloud when you scroll down on the left.

Editing Widget Parameters per Filter Function WordPress

If you want to change the parameters of the default widgets, for instance the font sizes of the tag cloud widget, you will notice that you can’t access the codes directly.

Editing the core files in the /wp-includes folder is obviously not an option.

Luckily, most of the default widgets are now applying filters to the parameters;
for example, in this the line the filter gets applied for the  tag cloud widget – in /wp-includes/default-widgets.php:

wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );

Add a filter function to functions.php of your theme;
for example changing the font size range for the tags:

(corrected 05 Feb 2013)

add_filter('widget_tag_cloud_args','set_tag_cloud_sizes');
function set_tag_cloud_sizes($args) {
$args = array_merge( $args, array('smallest' => 12, 'largest' => 19) );
return $args; }

Alternative code:

add_filter('widget_tag_cloud_args','set_tag_cloud_sizes');
function set_tag_cloud_sizes($args) {
$args['smallest'] = 12;
$args['largest'] = 19;
return $args; }

Another example – to show only tags from the current single post:

add_filter('widget_tag_cloud_args','single_post_tag_cloud_tags');
function single_post_tag_cloud_tags($args) {
if( is_single() && $args['taxonomy'] == 'post_tag' ) {
global $post;
$post_tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
$args = array_merge( $args, array('include' => implode(',',$post_tag_ids)) );
}
return $args;
}

See the codex chapter of wp_tag_cloud()   for all possible parameters.

Another similar example – excluding the default category ‘uncategorized’ from the category widget:

add_filter('widget_categories_args','exclude_default_cat');
function exclude_default_cat($args) {
$args['exclude'] = '1';
return $args;
}

A further example – removing the output of the category description as title attributes in the category widget:

add_filter('widget_categories_args','remove_cat_description_widget');
function remove_cat_description_widget($args) {
$args['use_desc_for_title'] = 0;
return $args;
}

Here is the list of all filters for the default widgets:

'widget_pages_args'
'widget_links_args'
'widget_archives_dropdown_args'
'widget_archives_args'
'widget_text'
'widget_categories_dropdown_args'
'widget_categories_args'
'widget_tag_cloud_args'