0

I've been writing up a function to assign classes to default WordPress widgets that I choose. After a bunch of research and modifications, I came up with the function below.

A quick explanation of what it does:


  • The variable, $classes, holds the class name I want to assign each type of widget to and each widget being assigned a class (ex. I want to add .main-blocks to the Archives and categories widget).

  • I use an if statement to match the widget types to assure that a class is only assigned to a widget I've listed in $classes. And because each widget gets a unique ID number as well, I've added $widget_num to check that too. For example, a category widget's HTML may get output like so: <li id="categories-2">...</li>

The problem: I'm thinking I'm not accessing the array properly, as this code doesn't do anything. I had a version of this working before using a few if/else statements (just to get things rolling), but I'm more about efficiency than just creating things to work.

When this works, the HTML of the widget should be like this, with classes assigned to my specified widgets:

https://i.stack.imgur.com/hFPI7.png

Thanks for the help!


function md_widget_classes($params) {
    global $wp_registered_widgets;
    $widget_id = $params[0]['widget_id'];
    $widget_obj = $wp_registered_widgets[$widget_id];
    $widget_num = $widget_obj['params'][0]['number'];
    $classes = array(
        'mini-blocks' => array('categories', 'archives'),
        'full-blocks' => array('recent-comments', 'recent-posts'),
        'blocks-list' => array('links', 'meta'));

    foreach($classes as $class => $widgets)
        foreach($widgets as $widget)
            if($params[0]['widget_id'] == "$widget-$widget_num")
                $params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']);
    return $params;
}
    add_filter('dynamic_sidebar_params', 'md_widget_classes');

1 Answer 1

1

The problem isn't how you are accessing the array, as you put it. It is with the str_replace line. That generates bad markup. You are replacing class=" with your classes, so <aside id="whatever" class="abc"> becomes <aside id="whatever" full-blocks">. That is just wrong. Try:

$params[0]['before_widget'] = preg_replace('/class="([^"]*)"/', 'class="'.$class.'"', $params[0]['before_widget']);

That should give you valid markup. I have concerns about completely replacing those classes. I'd consider adding to them instead. That is a pretty simple change:

$params[0]['before_widget'] = preg_replace('/class="([^"]*)"/', 'class="$1 '.$class.'"', $params[0]['before_widget']);

Notice the $1 right after the class=" in the replacement string.

2
  • Guess you can't trust all the code you get off the Internet, huh? Thanks for taking the time to look at this! :D Jan 17, 2013 at 1:04
  • You should watch out for mine too :)
    – s_ha_dum
    Jan 17, 2013 at 1:43

Your Answer

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

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