Excluding specific widgets from default sidebar class

I have two sidebars using the register_sidebars function that display each widget as a list item with a class of .box:

?php if(function_exists('register_sidebars')){

register_sidebars(2, array(
    'name'='sidebar%d',
    'id'  ='sidebar',
    'before_widget'='li class="box"',
    'after_widget'='/li!--box--!--aside--',
    'before_title'='h2',
    'after_title'='/h2',
));
}
?

So the output goes something like:

div class="side"
 ul
  li class="box"
   div class="textwidget"Lorem ipsum/div
  /li!--box--!--aside--
  li class="box"
   h2Blogroll/h2
   ul class="xoxo blogroll"
   lia href="http://example.com"Example/a/li
   /ul
  /li!--box--!--aside--
 /ul
/div

What I need is a conditional function so that any Links widget (or one with a class of .xoxo) in the sidebars will not get a class of .box because I want to style those and only those differently, and in a way that I can simply change certain styles from inside of .box. The main problem is that I don't know what function or variables to target with a filter. The codex doesn't do a good job of classifying less-used global variables and I can't see anything in the Filter Reference that looks like it should help.

Topic blogroll filters sidebar widgets Wordpress

Category Web


I coded my own answer together using a filter and dynamic_sidebar_params:

<?php

add_filter('dynamic_sidebar_params','io_blogroll_class');

function io_blogroll_class($params){
 $params[0]['before_widget'] =  '<li class="'.$params[0]['widget_name'].' box">';
 $params[0]['after_widget'] =  '</li><!--'.$params[0]['widget_name'].'-->';

    if ($params[0]['widget_name'] == "Links"){
$params[0]['before_widget'] =  '<li class="'.$params[0]['widget_name'].'">';
 $params[0]['after_widget'] =  '</li><!--'.$params[0]['widget_name'].'-->';
}   
return $params;
}

?>

The first lines actually override whatever you programmed register_sidebar() or register_sidebars()to put before and after a widget. So you can safely leave that blank in register_sidebar() if you're using this code. The first lines of the function make the name of the widget (therefore the type) a class of the list item and give a default class of .box to all widgets. The if statement basically says, "If any of the widgets have the class .Links (which they will if they're a blogroll or linklist) change the XHTML list item tags before and after these widgets to use their widget name as their only class."

I hope this code helps a few people who are staring down the same question.


You can actually try that using jQuery.

jQuery('.xoxo').parent().removeClass('box');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.