is_active_widget()
Defines whether the specified widget is displayed on the site (on the frontend). It gets the ID of the panel in which the widget is located.
The function retrieves all active widgets from all sidebars and searches for the specified widget.
Parameter $id_base - main
For searching the widget, it is most convenient to use the parameter $id_base. In it, you need to specify the widget ID, which is indicated in the first parameter of the WP_Widget constructor when creating the widget (see register_widget()).
Parameter $widget_id - narrows the search to a specific widget
But it determines what the function will return. For example, if the site displays the "Text" widget and it is called twice. Its base name is text ($id_base == 'text'). However, the widget IDs will be different, they are created as $id_base-$i++. That is, the $widget_id will be: text-1 and text-2. Further, if we do not specify the parameter $widget_id in the search:
is_active_widget( 0, 0, 'text' );
then the function will return the first found widget, regardless of whether it is text-1 or text-2. But if we specify the parameter $widget_id:
is_active_widget( 0, 'text-1', 'text' );
then the function will search for a specific widget: with base 'text' and ID 'text-1'.
At the same time, you cannot specify only the parameter $widget_id without $id_base. Then the function will return false. And I found this strange... Maybe they will fix it in the next versions. But in 4.7 it is still like this.
Parameter $callback - unnecessary
To search for a widget, instead of $id_base, you can specify the parameter $callback, but there you need to pass an array of all instances of callback functions. In short, it is unclear what to pass there, and in 99% of cases, it is easier to just forget about this parameter...
Uses the global variable $wp_registered_widgets which stores all registered widgets.
No Hooks.
Returns
String|false.
- ID of the sidebar, for example, sidebar-1 in which the active widget was found.
- false - if the widget is not active (not found in any sidebar).
- Will return wp_inactive_widgets if the parameter $skip_inactive is disabled and the found widget is inactive.
Usage
is_active_widget( $callback, $widget_id, $id_base, $skip_inactive );
- $callback(array/false)
- widget callback for checking. This is usually not used for searching the widget. Here you need to specify exactly the callback of the widget from the global variable $wp_registered_widgets.
Default: false - $widget_id(number/false)
- ID of the widget that needs to be found. The ID is created as
base ID + number-$id_base-$i++
Default: false - $id_base(string/false)
Base ID of the widget. The base is used to create a dynamic ID. The dynamic is needed because widgets can be used multiple times. This parameter is specified when registering the widget in the first parameter when extending the WP_Widget class (see register_widget()).
The base name of each widget that exists in WP by default can be found in the description of the function the_widget().
Default: false
- $skip_inactive(boolean)
- Set to false to search in inactive widgets as well. In this case, if the function finds a widget in inactive ones, it will return wp_inactive_widgets.
Default: true
Examples
#1 Check if the Calendar widget is active
$sidebar_id = is_active_widget( 0, 0, 'calendar' );
if( $sidebar_id ){
echo 'There is an active Calendar widget in the front';
} #2 Search in inactive widgets
Suppose we have a 'Text' widget, but it is not in any sidebar, and lies in an inactive area in the admin panel.
$sidebar_id = is_active_widget( 0, 0, 'text', 0 ); echo $sidebar_id; // wp_inactive_widgets
#3 Only load a script when the widget is active
add_action( 'wp_enqueue_scripts', function(){
if ( is_active_widget( false, false, $id_base, true ) ) {
wp_enqueue_script( 'jquery' );
}
} );
Notes
- Global. Array.
$wp_registered_widgetsThe registered widgets.
Changelog
| Since 2.2.0 | Introduced. |