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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.