Recently I had a client ask me to make the sidebar tag cloud widget be displayed in a list, but he wanted all the tags to be the same size, while I didn't know how to do this without creating my own widget I decided to do some research and thought that there could be some filter or function to help me out. While doing my research i did find a way to edit the output of the categories and blogroll lists using the Walker Class. But I found this to be way more complicated then I thought.

WordPress allows you to add widget areas where ever you want and while we can create custom widgets some times it's essential to be able to manipulate the arguments of the default ones. This more then being a tutorial is a guide of the filters we can use to manipulate the arguments that these widgets have.

It was kinda hard to find information about the hooks available to manipulate the widgets but I managed to find a few, not really sure if the complete list but there the most common to be used.
Here's the list of hooks available (along with the function they manipulate)

We can find all the hooks in the file default-widgets.php that's inside the folder wp-includes. Basically what the widgets do is use the default values of the template tags, by applying a filter to these widgets we can edit the options of the output.

So basically we know the way to apply a filter within our theme. We open our function.php file with in our theme and add a function along with the filter function. Here's the function i had to use for the client i mentioned earlier.

	function my_tag_filter($args) {
		$myarray = array(
				'format'    => 'list',
				'smallest'  => 12,
				'largest'   => 12
				);
		$args = array_merge($args, $myarray);
		return $args;
	}

So basically what i did here was create my own array with the modifications i wanted to apply to the widget. After you create your own array you need to merge it with the default arguments the widget uses. By using the PHP function "array_merge" we merge both our arrays. The thing about this function is that it replaces the elements we define in our custom array with the existing ones. So there's no chance of having an argument repeated.
After creating our filter function we need to make WordPress realize that we altered the arguments, this is where we actually apply the filter with the WordPress function "add_filter".

add_filter('widget_tag_cloud_args', 'my_tag_filter');

That's basically all that's needed to take control of our widget arguments, of course that the arguments are different for each of the widgets but this is to give you a small idea of how to use it. As you can see I linked each hook to the function it uses within the widgets.

Hope this helps someone out :)
Comments and questions are welcome as always!

2 Comments so far Leave your own

  1. Markus
    16 Dec, 2009

    thanks mate
    good tip

  2. Juri
    8 Jun, 2010

    1000 times THANK YOU!!!

Post Your Comment