How to add class on Widget Title

This is an example of register sidebar function from codex. My focus is on the before_title argument. What i want is to add a class to the title(<h3>). From my widget code or may be pass the class name from user input on widget area.

<?php $args = array(
    'name'          => sprintf(__('Sidebar %d'), $i ),
    'id'            => 'sidebar-$i',
    'description'   => '',
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h3>',
    'after_title'   => '</h3>' ); 

register_sidebar( $args );

?>

So, How do i add classes dynamically on widget header?

Related posts

Leave a Reply

1 comment

  1. Two approaches:

    1. Don’t bother

      Just target .widget.{dynamic-class} h3 instead

    2. Use %2$s

      I don’t know if this will work, but try:

      'before_title' => '<h3 class="%2$s">'

    But personally, I’d go with the former option. You’ve already got a unique class name; just leverage the element hierarchy to apply styles selectively.