How To Programmically Remove Widgets From Sidebar

You can manually remove Widgets through the admin settings, but to remove them as an option you would add the following to your child theme functions.php file

// Widget removal from admin
// https://premium.wpmudev.org/blog/how-to-remove-default-wordpress-widgets-and-clean-up-your-widgets-page/
// unregister all widgets
function unregister_default_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Text');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
unregister_widget('WP_Nav_Menu_Widget');
unregister_widget('Twenty_Eleven_Ephemera_Widget');
}
add_action('widgets_init', 'unregister_default_widgets', 11);

 

 

  1. // Widget removal from admin
  2. // https://premium.wpmudev.org/blog/how-to-remove-default-wordpress-widgets-and-clean-up-your-widgets-page/
  3. // unregister all widgets
  4. function unregister_default_widgets() {
  5. unregister_widget(‘WP_Widget_Pages’);
  6. unregister_widget(‘WP_Widget_Calendar’);
  7. unregister_widget(‘WP_Widget_Archives’);
  8. unregister_widget(‘WP_Widget_Links’);
  9. unregister_widget(‘WP_Widget_Meta’);
  10. unregister_widget(‘WP_Widget_Search’);
  11. unregister_widget(‘WP_Widget_Text’);
  12. unregister_widget(‘WP_Widget_Categories’);
  13. unregister_widget(‘WP_Widget_Recent_Posts’);
  14. unregister_widget(‘WP_Widget_Recent_Comments’);
  15. unregister_widget(‘WP_Widget_RSS’);
  16. unregister_widget(‘WP_Widget_Tag_Cloud’);
  17. unregister_widget(‘WP_Nav_Menu_Widget’);
  18. unregister_widget(‘Twenty_Eleven_Ephemera_Widget’);
  19. }
  20. add_action(‘widgets_init’, ‘unregister_default_widgets’, 11);

 

However, some may still appear in your sidebar depending on previous settings or theme settings.  To remove all Widgets from your sidebar, add the following to your child theme’s functions.php file.

// https://wordpress.stackexchange.com/questions/192159/remove-all-widgets-from-sidebar
add_filter( 'sidebars_widgets', 'disable_all_widgets' );

function disable_all_widgets( $sidebars_widgets ) {

  $sidebars_widgets = array( false );

  return $sidebars_widgets;
}