WordPress – widget title as class name in widget main div


Simply add this code in your functions.php file.

add_filter( 'widget_display_callback', 'add_widget_title_to_before_widget', 10, 3 );
function add_widget_title_to_before_widget( $instance, $widget_class, $args ) {
if ( ! empty( $instance['title'] ) ) {
$new_class = 'class="' . sanitize_title( $instance['title'] ) . ' ';
$args['before_widget'] = str_replace('class="', $new_class, $args['before_widget']);
$widget_class->widget( $args, $instance );
return false;
}
}

widget title as class name in widget main div

Advertisements

One thought on “WordPress – widget title as class name in widget main div

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s