Notice: get_settings is deprecated since version 2.1.0! Use get_option() instead. in /var/www/vhost/ on line 3839 Notice: get_settings is deprecated since version 2.1.0! Use get_option() instead. in /var/www/vhost/ on line 3839

1. Fake Widget Filter

Unfortunately, of the bajillion filters WordPress has, none of them apply to the meat of widgets. What’s worse, there isn’t a function to return individual widgets in order to manually manipulate their content. I have no idea why

I tried putting the opening <div> in the after_title arg and the closing </div> in the after_widget arg when registering the sidebar, (which would have been an easy solution), but after_header is not applied to widgets with blank titles (again, no idea why). So any widget without a title will get a closing <div> without an opening one, and that will break your layout. The only solution at this point is to use an object buffer.

About Object Buffers

Essentially, a object buffer will prevent content from being echoed normally, and will return it as a string. I’d never had occasion to use one before this, but it’s quite a handy tool. The only thing to be wary of is failing to clear the buffer. If you don’t, you may get buffered text from elsewhere which is unpredictable and generally a bad time.

Using Object Buffers

  1. Always use ob_clear() or ob_flush() before you begin to clear anything leftover in the buffer.
  2. Start the buffer with ob_start(). After this line, any output that would normally go right to the screen (like echo() or print_r()) gets trapped in the buffer instead.
  3. You can then use ob_get_contents() to return the currently buffered output to a variable without clearing the buffer.
  4. Alternatively, you can use ob_get_clean() or ob_get_flush() to return the currently buffered content to a variable, and stop the buffering process.

Adding a .widget-title CSS Class

Once you’ve captured the string with an object buffer, you can do whatever you want to it… you sicko. Personally, I think the most useful manipulation is to wrap everything under the .widget-title with a .widget-content container element. For me, working with 2 inner elements that wrap all the text is a lot more intuitive than a 1 inner element that only wraps the title and loose text in the remainder, that’s just silly.

// object buffer
ob_clean(); // CLEAR the object buffer
ob_start(); // start object buffer
	if( !dynamic_sidebar( 'sidebar_id' ) ) // get the dynamic sidebar if exists
	themeslug_default_sidebar(); // or the default sidebar if it doesn't
$output = ob_get_clean(); // get buffer contents and stop buffering

/* regular expression
 * note: This will only add the OPENING .widget-content <div>!
 * you need to specify a closing </div> in the 'after_widget' arg when
 * registering your sidebar. You could add this with a regex too, but
 * I suck at regular expression so I didn't
$pattern = '/<li(&#91;^>]*)class="widget([^"]*)"([^>]*)>(<h2)?(&#91;^>]*)?(class="widget-title")?([^>]*)?(>)?([^>]*)?(>)?/'; // the regex search pattern
$replace = '<li$1class="widget$2"$3>$4$5$6$7$8$9$10<div class="widget-content">';
$output = preg_replace( $pattern, $replace, $output );

// return or echo the new sidebar!
echo $output; 

Please Note

This regex only adds an opening <div class=".widget-content"> before the widget content, it does not close it. You should specify the closing div in the after_widget arg, when registering the sidebar.

The regex uses what I suspect to be a ridiculous search pattern of:


It got so complicated because of the unpredictability of a dynamic sidebar widgets, which may or may not have a title wrapped in a .widget-title element, and also because I’m terrible at regular expressions.

I’m quite sure there’s a more elegant regex to do the same thing, so if you’ve got one please leave a comment and I’ll update this. I will also give you credit and gratitude.

If you don’t mind the mess on the back end, a .widget-conent class makes styling widgets a lot more straight forward. Next up, the get_searchform() echo bug.

One Thought on “3 WordPress Hacks to Unlock Some Much-Needed Features

  1. Shariq on April 18, 2013 at 12:38 am said:


    Thanks for the great post. However, I am having trouble implementing it.
    I am trying to change the look of my searchform. It basically needs to have two text fields. I want this ti be in form of a plugin, which anyone can activate. SO obviously I do not have any control over searchform.php. This made me look at your code snippet.

    I copied your code as it is and added a filter like:
    However, the code seems to go in an infinite loop. I put an echo statement after lines 11 and 12. The one after 12 never gets printed while the one after line 11 keeps on getting printed continously.

    I tried changing the filter to pass the argument to:
    1. add_filter(‘get_search_form’,shinra_searchform(true),1);
    2. add_filter(‘get_search_form’,shinra_searchform(false),1);
    3. add_filter(‘get_search_form’,shinra_searchform($echo),1);
    But none of these work.

    Can you please help me out.

Leave a Reply

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

Post Navigation