1

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

<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

2 Answers 2

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;
}
4
  • Hi thanks for your answer. Its very helpful. Can you please tell me why you used $url variable? Mar 13, 2012 at 17:40
  • According to your after_title HTML code you did <a class="dropdown" style="float: right;" href="#%1$s" data-toggle="collapse">, where %1$s is a URL, so hypothetically I inserted a URL there. $url = esc_url( 'http://some-url-you-need.com' );
    – soulseekah
    Mar 13, 2012 at 17:57
  • Ahh got it.. I added your code in my functions.php file. But i'm not seeing any changes in my sidebar widget output. Is something wrong? And one more thing.. I can assign only href value to $url variable. What about the other attributes like class,style and data-toggle ? Mar 14, 2012 at 15:52
  • @Viruthagiri, please make sure you're actually targeting your widget if ( $id != 'mywidget' ) return $params; have you changed this line?
    – soulseekah
    Mar 14, 2012 at 16:54
1

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.