Wordpress - Add class to before_widget from within a custom widget
Aha, so the $before_widget
variable is a string representing div element: <div class="widget my" id="my-widget-1">
. So I checked the $before_widget
for the "class" sub-string and added my $widget_width
value to it.
The code is from my custom widget file:
function widget( $args, $instance ) {
extract( $args );
... //other code
$widget_width = !empty($instance['widget_width']) ? $instance['widget_width'] : "col300";
/* Add the width from $widget_width to the class from the $before widget */
// no 'class' attribute - add one with the value of width
if( strpos($before_widget, 'class') === false ) {
// include closing tag in replace string
$before_widget = str_replace('>', 'class="'. $widget_width . '">', $before_widget);
}
// there is 'class' attribute - append width value to it
else {
$before_widget = str_replace('class="', 'class="'. $widget_width . ' ', $before_widget);
}
/* Before widget */
echo $before_widget;
... //other code
}
I wanted to add my $widget_width
variable to the widget div element within my own widget code (while I had an easy access to the $widget_width
variable).
Hope that makes sense and will help someone.
Thanks, Dasha
you can use dynamic_sidebar_params
filter hook to find your widget and add your classes to it:
add_filter('dynamic_sidebar_params', 'add_classes_to__widget');
function add_classes_to__widget($params){
if ($params[0]['widget_id'] == "my-widget-1"){ //make sure its your widget id here
// its your widget so you add your classes
$classe_to_add = 'col480 whatever bla bla '; // make sure you leave a space at the end
$classe_to_add = 'class=" '.$classe_to_add;
$params[0]['before_widget'] = str_replace('class="',$classe_to_add,$params[0]['before_widget']);
}
return $params;
}
Another way I found to add a class for a custom widget is to use the the 'classname' key of your construct function like in:
class My_Widget_Class extends WP_Widget {
// Prior PHP5 use the children class name for the constructor…
// function My_Widget_Class()
function __construct() {
$widget_ops = array(
'classname' => 'my-class-name',
'description' => __("Widget for the sake of Mankind",'themedomain'),
);
$control_ops = array(
'id_base' => 'my-widget-class-widget'
);
//some more code after...
// Call parent constructor you may substitute the 1st argument by $control_ops['id_base'] and remove the 4th.
parent::__construct(@func_get_arg(0),@func_get_arg(1),$widget_ops,$control_ops);
}
}
And be sure to use default 'before_widget' in your theme or if you use register_sidebar()
in function.php, do it like this:
//This is just an example.
register_sidebar(array(
'name'=> 'Sidebar',
'id' => 'sidebar-default',
'class' => '',//I never found where this is used...
'description' => 'A sidebar for Mankind',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',//This is the important code!!
'after_widget' => '</aside>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
Then on every instances of your widget, you will have the class 'widget my-class-name' like this:
<aside class="widget my-class-name" id="my-widget-class-widget-N"><!-- where N is a number -->
<h3>WIDGET TITLE</h3>
<p>WIDGET CONTENT</p>
</aside>
You may also call the parent constructor first and then append whatever class name you want :
class My_Widget_Class extends WP_Widget {
// Better defining the parent argument list …
function __construct($id_base, $name, $widget_options = array(), $control_options = array())
{ parent::__construct($id_base, $name, $widget_options, $control_options);
// Change the class name after
$this->widget_options['classname'].= ' some-extra';
}
}