Last Updated: February 25, 2016
·
5.574K
· leonardodna

Wordpress widget title as class name

Sometimes we get a layout that need a css style for each widget title, but Wordpress doesn't have an automatic way to do this on register_sidebar(). So, we need to use this filter on functions.php to do the trick:

function widget_title_as_class($title) { 
    return '<h3 class="widget-title ' . sanitize_title($title) . '">' . $title . '</h3>';
}
add_filter('widget_title', 'widget_title_as_class');

2 Responses
Add your response

Is there also a way to add a specific class to the widget itself? So for example add the title of the widget as a class to the entire widget.

over 1 year ago ·

@dalenberg Yes, there's a way to change the widget class. As it is a little tricky to do, I've created another tip => https://coderwall.com/p/hynjha

over 1 year ago ·