WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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');
share|improve this question
up vote 1 down vote accepted

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.

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

Your Answer


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.