unregister_widget()
Unregisters a widget.
Unregisters a WP_Widget widget. Useful for un-registering default widgets. Run within a function hooked to the widgets_init action.
No Hooks.
Return
null
. Nothing (null).
Usage
unregister_widget( $widget );
- $widget(string|WP_Widget) (required)
- Either the name of a WP_Widget subclass or an instance of a WP_Widget subclass.
Examples
#1 Delete widget
This example shows how to unregister the Calendar widget. It may come in handy when you need to replace the standard widget with your own:
add_action( 'widgets_init', 'remove_calendar_widget' ); function remove_calendar_widget() { unregister_widget('WP_Widget_Calendar'); }
#2 Disable all standard WordPress widgets
To disable all basic widgets in WordPress, use this code:
// Disable standard WordPress widgets add_action('widgets_init', 'unregister_basic_widgets' ); function unregister_basic_widgets() { unregister_widget('WP_Widget_Pages'); // Page widget unregister_widget('WP_Widget_Calendar'); // Calendar unregister_widget('WP_Widget_Archives'); // Archives unregister_widget('WP_Widget_Links'); // Links unregister_widget('WP_Widget_Meta'); // Meta widget unregister_widget('WP_Widget_Search'); // Search unregister_widget('WP_Widget_Text'); // Text unregister_widget('WP_Widget_Categories'); // Categories unregister_widget('WP_Widget_Recent_Posts'); // Recent posts unregister_widget('WP_Widget_Recent_Comments'); // Recent comments unregister_widget('WP_Widget_RSS'); // RSS unregister_widget('WP_Widget_Tag_Cloud'); // Tag cloud unregister_widget('WP_Nav_Menu_Widget'); // Menu unregister_widget('WP_Widget_Media_Audio'); // Audio unregister_widget('WP_Widget_Media_Video'); // Video unregister_widget('WP_Widget_Media_Gallery'); // Gallery unregister_widget('WP_Widget_Media_Image'); // Image }
Use this code in the theme file functions.php.
#3 Disabling widget support in WordPress
The example above shows how to disable all widgets by default and gives you a choice of which widgets to disable and which not.
But if you need to disable absolutely all widgets at once, it is better to do it before they are connected, i.e. not to connect them at all and not to call event which connects other widgets. To do this, detach function wp_widgets_init() from event init
, like this:
remove_action( 'init', 'wp_widgets_init', 1 );
Besides connection of widgets, function wp_widgets_init() calls event widgets_init to which hooks to enable widgets from plugins are usually hung. So when you disable this hook, most likely the plugin widgets and themes will not work. I.e., widget support will be disabled completely.
#4 Fundamental disabling of basic widgets
This example shows how to disable basic WordPress widgets. But it must be done in a way that disables the PHP plugin widget files and after disabling, the base widget classes do not exist: WP_Widget_Pages, WP_Widget_Search, etc. After this disabling, we can create our own widgets with the names of the base classes. This is, if I may say so, disabling the base widgets at the root.
Since this is a low-level setting, such code should be either in the regular plugin or in mandatory plugin, but not later.
remove_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 ); // turn on the event disabled in wp_maybe_load_widgets add_action( '_admin_menu', 'wp_widgets_add_menu' ); // not to register widgets of classes that now do not exist at all... remove_action( 'init', 'wp_widgets_init', 1 ); // turn on the event disabled in wp_widgets_init add_action( 'init', 'my_wp_widgets_init', 1 ); function my_wp_widgets_init(){ if ( !is_blog_installed() ) return; /** * Fires after all default WordPress widgets have been registered. * * @since 2.2.0 */ do_action( 'widgets_init' ); }
After installing this code, all base widgets will be disabled, but the widget menu in the admin panel will remain working... And you can also write your own widgets with the names of PHP classes of the base widgets: WP_Widget_Pages, WP_Widget_Search, etc.
Notes
- See: WP_Widget
- Global. WP_Widget_Factory. $wp_widget_factory
Changelog
Since 2.8.0 | Introduced. |
Since 4.6.0 | Updated the $widget parameter to also accept a WP_Widget instance object instead of simply a WP_Widget subclass name. |
unregister_widget() unregister widget code WP 6.7.1
function unregister_widget( $widget ) { global $wp_widget_factory; $wp_widget_factory->unregister( $widget ); }