ryeohq
Last Updated: February 25, 2016
·
4.49K
· leonardodna
44c5fbea96c3b8c76a0ae4b266f058c9

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');
Say Thanks
Respond

2 Responses
Add your response

4973
71f2570d45cfbbbff1ce1d8a92a68ef9

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 ·
5018
44c5fbea96c3b8c76a0ae4b266f058c9

@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 ·