59 | Editing Widget Parameters per Filter Function WordPress

g+

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'
This entry was posted in Easy Coding for Wordpress, Tips & Tricks, WordPress and tagged , , , , , . Bookmark the permalink.

3 Responses to Editing Widget Parameters per Filter Function WordPress

  1. Heather comments:

    Hi,
    Thanks for the helpful post! This worked perfectly for me.

  2. Tabitha comments:

    Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Here is my blog post; Best Eye cream for dark circles

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>