3

Does someone know a solution for this?

I want to remove (not unregister) all widgets from a sidebar with a function.

2 Answers 2

5

You could add this function to your functions.php file.

add_filter( 'sidebars_widgets', 'disable_all_widgets' );

function disable_all_widgets( $sidebars_widgets ) {

  $sidebars_widgets = array( false );

  return $sidebars_widgets;
}

You could also use the Wordpress conditional tags to disable widgets only on certain pages. For example; this would only disable widgets on the home page.

add_filter( 'sidebars_widgets', 'disable_all_widgets' );

function disable_all_widgets( $sidebars_widgets ) {

    if ( is_home() )
        $sidebars_widgets = array( false );

    return $sidebars_widgets;
}
0
2

There is a quick way to accomplish this via jQuery and the developer console. First, navigate to Appearance Widgets and open the developer console (I use Chrome so I just hit CTRL+SHIFT+J on the keyboard to open it). This action cannot be undone, so be careful and make a database backup first.

$('.button-link.button-link-delete.widget-control-remove').click();

What this is doing is using a CSS selector to get a list of all 'Delete' buttons/links on the page and simulating a click on it.

This leaves your sidebars registered, but it empties all widgets that are added.

I used it today because a theme I installed pre-populated all the sidebars with tons of widgets that were causing the site to load slowly even though they were only used on certain pages.

Note: for some reason, this will appear to remove even the widget templates on the left. It is not. Just refresh the Widget management page and they will all be there, available to add to the sidebars.

1
  • 1
    This is very useful when you need to delete all widgets in dev console. Thanks! Oct 23, 2018 at 14:21

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.