Advertisements

Perform changes on existing widgets.

The Widget API is pretty nice and there are tons of existing widgets out there which are ready to use, but sometimes it just needs a little change to make things perfect. Here is how you can alter the widgets’ output without touching the widget code.

Simply put something like this in your themes’ functions.php file or other appropriate place.

add_filter( 'widget_display_callback', 'capture_widget', 100, 3 );
function capture_widget( $instance, $widget_class, $args ) {
	if ( $instance['title'] != 'My Widget Title' )
		return $instance;
	// alternatively you could also use $widget_class->name as conditional parameter
	
	ob_start();
	$widget_class->widget( $args, $instance );
	$result = ob_get_clean();
	// do preg replace magic on result
	$result = preg_replace( '/foo/', 'bar', $result );
	echo $result;
	
	return false;
}
Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: