Toggle option in sidebar widgets

I would like to use toggle option in my sidebar widgets. But my sidebar widgets not generating unique id.
This is my register sidebar code.

register_sidebar(array(
        'name' => 'Main Sidebar',
        'before_widget' => '<div class="widget %2$s">',
        'after_widget' => '</div></div></div>',
        'before_title' => '<div class="titlediv">',
        'after_title' => '<a class="dropdown" style="float: right;" href="#%1$s" data-toggle="collapse"><b class="toggleicon"></b></a></div><div id="%1$s" class="collapse in"><div id="%1$s" class="boxdiv">'
    )

This is how i’m getting output

Read More
<div class="widget widget_stc-followers">       
<div class="titlediv">Widget Title<a class="dropdown" style="float: right;" href="#%1$s" data-toggle="collapse"><b class="toggleicon"></b></a></div><div id="%1$s" class="collapse in"><div id="%1$s" class="boxdiv">       
Widget content
</div></div></div>

I mean %1$s not working in after_title. I want to use unique id. So can anyone give me some tips to make it work?
Thanks

Related posts

Leave a Reply

2 comments

  1. The after_title does not go through any transformations. In fact only the before_widget does:

    http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/widgets.php#L876

    However, a little lower you can see that $params = apply_filters( 'dynamic_sidebar_params', $params ); there’s a filter you can hook to do your own filtering.

    add_filter( 'dynamic_sidebar_params', 'wpse_45418_change_after_title' );
    function wpse_45418_change_after_title( $params ) {
    
        $id = $params[0]['widget_id'];
    
        if ( $id != 'mywidget' ) return $params;
    
        global $wp_registered_widgets;
        foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
            if ( is_string($cn) )
                $classname_ .= '_' . $cn;
            elseif ( is_object($cn) )
                $classname_ .= '_' . get_class($cn);
        }
    
        $params[0]['after_title'] = sprintf($params[0]['after_title'], $id, $classname );
    
        return $params;
    }
    

    This is the general answer to why your IDs are not getting rendered. From this example, to generate your own unique IDs in the after_title key, you would need to do something similar, but provide unique IDs, like so:

    add_filter( 'dynamic_sidebar_params', 'wpse_45418_change_after_title' );
    function wpse_45418_change_after_title( $params ) {
    
        $id = $params[0]['widget_id'];
        if ( $id != 'mywidget' ) return $params;
    
        $url = esc_url( 'http://some-url-you-need.com' );
        $uidcollapse = $id.'-unique-id';
        $uidboxdiv = $id.'-another-unique-id';
    
        // <a class="dropdown" style="float: right;" href="%1$s" data-toggle="collapse"><b class="toggleicon"></b></a></div><div id="#%2$s" class="collapse in"><div id="#%3$s" class="boxdiv">
    
        $params[0]['after_title'] = sprintf($params[0]['after_title'], $url, $classname, $uidcollapes, $uidboxdiv );
    
        return $params;
    }
    
  2. Note: Id needs to be unique and you are trying to set the same id to 2 different div’s.

    Now id %1$s and class %2$s attributes only work in the before_widget argument as far as i know but you can use to toggle you widgets based on but with a bit more then just data-toggle="collapse".