Using WordPress ‘is_active_widget()’ PHP function

The is_active_widget() WordPress PHP function determines whether a given widget is displayed on the front end.

Usage

is_active_widget($callback, $widget_id, $id_base, $skip_inactive);

Parameters

  • $callback (callable|false) – Optional. Widget callback to check. Default: false
  • $widget_id (string|false) – Optional. Widget ID. Optional, but needed for checking. Default: false
  • $id_base (string|false) – Optional. The base ID of a widget created by extending WP_Widget. Default: false
  • $skip_inactive (bool) – Optional. Whether to check in ‘wp_inactive_widgets’. Default: true

More information

See WordPress Developer Resources: is_active_widget()

Examples

Check if a specific widget is active

This example checks if a widget with the given $callback function is active.

if (is_active_widget(false, false, 'my_custom_widget', true)) {
    // Do something if the widget is active
}

Check if a widget with a specific ID is active

This example checks if a widget with the given $widget_id is active.

if (is_active_widget(false, 'my_widget_id', false, true)) {
    // Do something if the widget is active
}

Load a script when a specific widget is active

This example enqueues a script only when a widget with the given $id_base is active.

if (is_active_widget(false, false, 'my_widget_base', true)) {
    wp_enqueue_script('jquery');
}

Check if a widget is active, including inactive widgets

This example checks if a widget with the given $id_base is active, including inactive widgets.

if (is_active_widget(false, false, 'my_widget_base', false)) {
    // Do something if the widget is active
}

Check if any widget with a specific callback is active

This example checks if any widget with the given $callback function is active.

if (is_active_widget('my_widget_callback', false, false, true)) {
    // Do something if the widget is active
}