WordPress at a glance

wp_unregister_sidebar_widget() WP 2.2.0

Unregisters (removes) a widget by its ID.

  • This function also removes a widget from the Appearance -> Widgets panel if you use it in the theme functions.php file.

  • You can find out the ID of the widget in its id attribute in the Widgets panel. For example, a widget with ID your_widget_1 will have id="widget-17_your_widget_1" attribute. And a block of some other widget with, for example, with ID my_widget will have id="widget-18_my_widget" attribute.
Hooks from the function
Return

Null. Nothing.

Usage

wp_unregister_sidebar_widget( $id );
$id(int/string) (required)
Widget ID which was specified when registering a widget with wp_register_sidebar_widget().

Examples

#1 Disable (unregister) a specific widget on all is_category() pages

Suppose we have registered my_widget widget in functions.php file using wp_register_sidebar_widget() function:

function my_widget_display(){
	// Widget output
}
wp_register_sidebar_widget(
	'my_widget',          // The widget ID
	'My widget',          // The tidget title
	'my_widget_display',  // The widget callback
);

Then we have added this widget to the right-sidebar. This sidebar is called in sidebar.php:

<ul id="sidebar">
	<?php dynamic_sidebar( 'right-sidebar' ); ?>
</ul>

Now, to disable my_widget on all categories pages, we need to add the following code before displaying the sidebar:

<?php wp_unregister_sidebar_widget( 'my_widget' ); ?>

<ul id="sidebar">
	<?php dynamic_sidebar( 'right-sidebar' ); ?>
</ul>

Changelog

Since 2.2.0 Introduced.

Code of wp unregister sidebar widget: wp-includes/widgets.php WP 5.2.4

<?php
function wp_unregister_sidebar_widget( $id ) {

	/**
	 * Fires just before a widget is removed from a sidebar.
	 *
	 * @since 3.0.0
	 *
	 * @param int $id The widget ID.
	 */
	do_action( 'wp_unregister_sidebar_widget', $id );

	wp_register_sidebar_widget( $id, '', '' );
	wp_unregister_widget_control( $id );
}

Related Functions

From tag: Sidebar (widgets)

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register