How To Remove Widgets From WordPress Properly

Here’s some code which you can add to your functions.php file to remove any build up of inactive widgets you no longer need in your WordPress installation.

$widgets = get_option('sidebars_widgets');
$widgets['wp_inactive_widgets'] = array();
update_option('sidebars_widgets', $widgets);

Thanks to One Trick Pony from WordPress Stackexchange for this code.

The code might not work in some child themes functions.php files so you may need to add it in your parent themes functions.php file instead.

If you do, i suggest you remove it after your widgets have been removed.

If you’re testing plugins with widgets on a regular basis, this code snippet will come in handy.

Limit Inactive Widgets by Number

You can also limit the amount of inactive widgets.

Remove WordPress Core Widgets Plugin

There’s also a plugin.

Remove Native Widgets

You may also like to remove the widgets which are included by default in all WordPress installations.

Note: Only remove the widgets that are core to WordPress and the ones you know have been registered otherwise you use this code:

remove_action( 'init', 'wp_widgets_init', 1 );
add_action( 'init', function() { do_action( 'widgets_init' ); }, 1 );

Here’s the code to unregister specific core widgets:

function remove_wordpress_widgets() {
    unregister_widget('WP_Widget_Pages');
    unregister_widget('WP_Widget_Calendar');
    unregister_widget('WP_Widget_Archives');
    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');
}
add_action('widgets_init', 'remove_wordpress_widgets', 1);

Simply delete widgets from this code block if you want to retain some of the default widgets or remove others which are also included in the core files.

Remove Genesis Theme Widgets

If you’re using the Genesis theme framework, you’ve got even more widgets to choose from.

But may be you don’t use them or you only use some of them. Simply modify this code to remove the Genesis widgets you don’t want to display and use in your Widgets page.

add_action( 'widgets_init', 'remove_genesis_widgets', 20 );
function remove_genesis_widgets() {
    unregister_widget( 'Genesis_eNews_Updates' );
    unregister_widget( 'Genesis_Featured_Page' );
    unregister_widget( 'Genesis_User_Profile_Widget' );
    unregister_widget( 'Genesis_Menu_Pages_Widget' );
    unregister_widget( 'Genesis_Widget_Menu_Categories' );
    unregister_widget( 'Genesis_Featured_Post' );
    unregister_widget( 'Genesis_Latest_Tweets_Widget' );
}

This code will un-register all default Genesis widgets from loading.

Remove Widgets On Specific Pages & Posts

You can also create a custom function and remove widgets from displaying on a specific page from loading using conditional tags.

add_filter ('sidebars_widgets', 'remove_pages_widget');
function remove_pages_widget( $sidebars_widgets ){
	if (is_page('007'))
		$sidebars_widgets ['sidebar'] = false;
	return $sidebars_widgets;
}

This code will work with any theme and should be placed at the end of your child themes functions.php file.

Simply change the conditional tag to remove sidebar widgets based on your own needs.

Here’s some code to remove widgets from displaying on your home page:

add_filter( 'sidebars_widgets', 'remove_home_widgets' );

function remove_home_widgets( $sidebars_widgets ) {
	if ( is_home() )
		$sidebars_widgets = array( false );
	return $sidebars_widgets;
}

Be aware that this code also removes your footer, header and any other widgets you have on that page. Not just the sidebar widgets.

Remove Sidebar Widgets On Specific Pages

You can also remove your sidebar from the home page and any other pages using CSS code:

.home #content,
.home #sidebar {
 display: none;
}

This CSS code will remove the primary sidebar from the home page.

To remove the sidebar from a page, post or archive, use a custom class, post or category i.d which you can grab using Firebug or by viewing the source code.

Remove Dashboard Widgets

This code will remove all the dashboard widgets like recent drafts and recent comments etc from displaying.

Remove Default Widgets Now & Forever

Another option is to install a plugin which prevents WordPress from registering any widgets in the first place.

The Remove Default Widgets plugin does this so you won’t need to modify any code at any time in the future.

Grab the code from Github if its not displaying.

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.