Does someone know a solution for this?
I want to remove (not unregister) all widgets from a sidebar with a function.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou 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;
}
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.