I've replaced the WP search on my site with a Google CSE. Google has a nice feature that lets you specify that certain areas of the page should not be indexable, by adding a "nocontent" class to those sections. I need to add this class to all the sidebars.

I've tried adding a filter to register_sidebar, and the data is changed in the function, but the change that I make is not passed into the output.

add_action( 'register_sidebar', 'uft_add_nocontent_class' );

function uft_add_nocontent_class( $sidebar_info ) {
  $sidebar_info['before_widget'] = str_replace('class="widget ','class="widget nocontent ',$sidebar_info['before_widget']);
  return $sidebar_info;

I also tried this with an action with similar lack of success. Should I just modify the child theme, or is there another way to make the hook work?

2 Answers 2


If you want to add a class before each widget you should use the dynamic_sidebar_params filter. Another post explains this well. Here's the gist of it.

function uft_add_nocontent_class($params) {
    $params[0]['before_widget'] = '<aside id="%1$s" class="widget %2$s nocontent">';
    return $params;
add_filter('dynamic_sidebar_params', 'uft_add_nocontent_class');

Otherwise, go and edit your child theme files. If you don't have a lot of sidebars that's probably the easiest. You could also use some JavaScript to add the class to your selected sidebars.

  • Thanks. Javascript won't work in this case because Googlebot doesn't execute it.
    – Joel
    May 19, 2015 at 12:43

You should probably be doing this when it's being displayed, not when the sidebar is registered. For example, sidebar.php or whatever variant your theme is using for the sidebar in question. You're probably attempting to change it before it's been set.

Your Answer

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

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