Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add a div to widget's content in my dynamic sidebar.

Here is the register code;

register_sidebar(array(
        'name' => "Sidebar1",
        'id' => 'home-sidebar-1',
        'before_widget' => '<div class="sidebar-box">',
        'after_widget' => '</div>',
        'before_title' => '<div class="title">',
        'after_title' => '</div>',
    ));

But this code causes like this;

<div class="sidebar-box">
    <div class="title">{WIDGET TITLE}</div>
     {WIDGET CINTENT}
</div>

Here is what i trying to do;

<div class="sidebar-box">
    <div class="title">{WIDGET TITLE}</div>
    <div class="content">
           {WIDGET CINTENT}
    </div> 
</div>

How it can be done?

share|improve this question

2 Answers

up vote 1 down vote accepted

In addition to Toscho's answer here's what you need for a robust solution:

// if no title then add widget content wrapper to before widget
add_filter( 'dynamic_sidebar_params', 'check_sidebar_params' );
function check_sidebar_params( $params ) {
    global $wp_registered_widgets;

    $settings_getter = $wp_registered_widgets[ $params[0]['widget_id'] ]['callback'][0];
    $settings = $settings_getter->get_settings();
    $settings = $settings[ $params[1]['number'] ];

    if ( $params[0][ 'after_widget' ] == '</div></div>' && isset( $settings[ 'title' ] ) && empty( $settings[ 'title' ] ) )
        $params[0][ 'before_widget' ] .= '<div class="content">';

    return $params;
}

From Toscho's answer:

register_sidebar(array(
    'name' => "Sidebar1",
    'id' => 'home-sidebar-1',
    'before_widget' => '<div class="sidebar-box">',
    'after_widget' => '</div></div>',
    'before_title' => '<div class="title">',
    'after_title' => '</div><div class="content">',
));

What this does is:

  • checks the settings for the registered sidebar for widgets ending in 2 closing <div>s
  • checks if there's no title
  • if not, it modifies the before_widget output to show the opening widget content div

You could use this approach to change the sidebar arguments in other ways to based on the widget instance's settings.

share|improve this answer
I have a problem with that: some widgets (like WP default ones, for example) set a default title if you leave that blank. So your function adds the div because there isn't a title in the params at that point, but the widget adds it afterwards and the code is messed up. Do you know how to check for the title from "within" the widget? Thanks – Cmorales Jan 25 at 1:36
Not without adding a widget by widget check or some horrendously complex code to render the widget silently then parse it for a title... Will have a think – sanchothefat Jan 25 at 17:07
Maybe hooking to this codex.wordpress.org/Function_Reference/the_widget would help? They use it (for other reason) in this article wp.smashingmagazine.com/2012/12/11/… – Cmorales Jan 29 at 0:53
1  
the_widget() isn't used in the dynamic_sidebar() function, it's a means of calling any widget with options you specify in place. It's another place to filter though so my solution doesn't cover that eventuality. Cheers – sanchothefat Jan 29 at 17:14

Add the second div to the title parameter:

register_sidebar(array(
        'name' => "Sidebar1",
        'id' => 'home-sidebar-1',
        'before_widget' => '<div class="sidebar-box">',
        'after_widget' => '</div></div>',
        'before_title' => '<div class="title">',
        'after_title' => '</div><div class="content">',
    ));
share|improve this answer
That fails if there's no widget title - it can be done though – sanchothefat Dec 3 '12 at 10:15
Yes it fails when there is no title.. – MBraiN Dec 3 '12 at 12:06
I've added an answer with some code to fix the titles problem. Should probably be merged with Toscho's really but he has epic rep already :) – sanchothefat Dec 3 '12 at 12:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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