Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to create a widget where it can make changes to the container?

An example is like this:

    <!--- widget-container --->
    <div class="wdget">
       <div class="textwidget">
         <!--TOPWIDGET-->
         <div style="clear:both;"></div> <!--- <=== See this =--->
         <!--/TOPWIDGET-->
      </div>
    </div>
    <!--- /widget-container --->

be like this:

    <!--- widget-container --->
         <div style="clear:both;"></div> <!--- <=== See this =--->
    <div class="wdget">
       <div class="textwidget">
      </div>
    </div>
    <!--- /widget-container --->

or something else like that.

If I use this:

    register_sidebar(
       array(
          'before_widget' => "<div style=\"clear:both;\">"
                            ."</div><div id=\"%1$s\" class=\"widget %2$s\">",
          'after_widget' => "</div>"
       )
    );

will replace all widget, not just a widget that I want.

My main goal is that I can give <div style="clear:both;"></div> element among a widget that I can treat as a widget, so it is easily to move.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I would not recommend that. Much easier to use a clearfix hack to force clearing and just add that style to your widget. Clearfix hacks are transparent: it doesn't matter if they happen on all elements there is no visible difference, other than clear floats.

Somewhere in your themes style.css:

.widget:before,
.widget:after {
    content: " ";
    display: table;
}

.widget:after {
    clear: both;
}

.widget {
    *zoom: 1;
}

You can, of course, put that anywhere. You could hook into wp_head or wp_footer and spit out a custom style tag just for that. Or you could be more specific. For instance if you're registering a custom widget, you could add a clearfix hack to that specifically by classname.

Alternatively, if you really want to just add a clear div to a certain widget, you can hook into dynamic_sidebar_params. Your hooked function will receive a single argument: an array of sidebar params -- including before_widget and after_widget and widget_name which will be the second argument you passed to parent::__construct when you register the widget. Something like this:

<?php
add_filter('dynamic_sidebar_params', 'wpse90953_sidebar_params');
function wpse90953_sidebar_params($widget_params)
{
    if (!empty($widget_params['widget_name']) && 'your_widget_name' === $widget_params['widget_name']) {
        $widget_params['before_widget'] = '<div style="clear:both"></div>' . $widget_params['before_widget'];
    }

    return $widget_params;
}
share|improve this answer
    
Good idea. But unfortunately, ['widget_name'] can be problematic when used in multilingual theme. In addition, your script lacking closing function empty() and $widget_params used in index 0. But off the top of all, thank you for your help, it really helped me. –  bayuah Mar 16 '13 at 1:06
    
$widget_params used in index 0, so this supposed to be $widget_params[0]['widget_name'] and $widget_params[0]['before_widget'] (I'm sorry, my comment previously incomplete :D ). –  bayuah Mar 16 '13 at 1:51
add comment

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.