On this page
is_active_widget( callable|false $callback = false, string|false $widget_id = false, string|false $id_base = false, bool $skip_inactive = true ): string|false
Determines whether a given widget is displayed on the front end.
Description
Either $callback or $id_base can be used $id_base is the first argument when extending WP_Widget class Without the optional $widget_id parameter, returns the ID of the first sidebar in which the first instance of the widget with the given callback or $id_base is found.
With the $widget_id parameter, returns the ID of the sidebar where the widget with that callback/$id_base AND that ID is found.
NOTE: $widget_id and $id_base are the same for single widgets. To be effective this function has to run after widgets have initialized, at action ‘init’ or later.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
$callbackcallable|false Optional-
Widget callback to check.
Default:
false $widget_idstring|false Optional-
Widget ID. Optional, but needed for checking.
Default:
false $id_basestring|false Optional-
The base ID of a widget created by extending WP_Widget.
Default:
false $skip_inactivebool Optional-
Whether to check in
'wp_inactive_widgets'.
Default:
true
Return
string|false ID of the sidebar in which the widget is active, false if the widget is not active.
Source
File: wp-includes/widgets.php. View all references
function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) {
global $wp_registered_widgets;
$sidebars_widgets = wp_get_sidebars_widgets();
if ( is_array( $sidebars_widgets ) ) {
foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
continue;
}
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] === $callback ) || ( $id_base && _get_widget_id_base( $widget ) === $id_base ) ) {
if ( ! $widget_id || $widget_id === $wp_registered_widgets[ $widget ]['id'] ) {
return $sidebar;
}
}
}
}
}
}
return false;
}
Related
Uses
| Uses | Description |
|---|---|
| wp_get_sidebars_widgets() wp-includes/widgets.php | Retrieve full list of sidebars and their widget instance IDs. |
| _get_widget_id_base() wp-includes/widgets.php | Retrieves the widget ID base value. |
Used By
| Used By | Description |
|---|---|
| wp_list_widgets() wp-admin/includes/widgets.php | Display list of the available widgets. |
| WP_Widget_Recent_Comments::__construct() wp-includes/widgets/class-wp-widget-recent-comments.php | Sets up a new Recent Comments widget instance. |
| WP_Customize_Widgets::get_available_widgets() wp-includes/class-wp-customize-widgets.php | Builds up an index of all available widgets for use in Backbone models. |
Changelog
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/is_active_widget