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');
Written by Leonardo Prado
Related protips
2 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#