is_active_widget( callable|false $callback = false, string|false $widget_id = false, string|false $id_base = false, bool $skip_inactive = true ): string|false
- Since
- 2.2.0
- Source
wp-includes/widgets.php:916
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 https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags article in the Theme Developer Handbook.
Compatibility
- WordPress
- since 2.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$callbackcallable|falseoptional- Widget callback to check. Default false.Default:
false $widget_idstring|falseoptional- Widget ID. Optional, but needed for checking.
Default false.Default:false $id_basestring|falseoptional- The base ID of a widget created by extending WP_Widget.
Default false.Default:false $skip_inactivebooloptional- Whether to check in 'wp_inactive_widgets'.
Default true.Default:true
Return value
string|false- ID of the sidebar in which the widget is active, false if the widget is not active.
Performance profile
How much work a call to is_active_widget() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 11–47
- Plugin surface
- None
- Called by
- 4
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 51.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()one call below is_active_widget() - hookthird-party callbacks
apply_filters()one call below is_active_widget()
Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost is_active_widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–41 | wp_get_sidebars_widgets() |
is_array($sidebars_widgets) && !$sidebars_widgets && is_array($widgets) && !$widgets && $id_base === false | 29–47 | wp_get_sidebars_widgets(), _get_widget_id_base() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 51 | 11–47 | 17 | |
| 8.5 | 51 | 11–47 | 17 | |
| 8.4 | 51 | 11–47 | 17 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 54 | 11–50 | 17 | |
| 8.2 | 54 | 11–50 | 17 | |
| 8.1 | 54 | 11–50 | 17 | |
| 7.4 | 54 | 11–50 | 17 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- wp_get_sidebars_widgets()Retrieves the full list of sidebars and their widget instance IDs.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- _get_widget_id_base()Retrieves the widget ID base value.
Used by · 4
- Twenty_Fourteen_Ephemera_Widget::__construct()Constructor.
- WP_Customize_Widgets::get_available_widgets()Builds up an index of all available widgets for use in Backbone models.
- WP_Widget_Recent_Comments::__construct()Sets up a new Recent Comments widget instance.
- wp_list_widgets()Display list of the available widgets.
Source code
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 || str_starts_with( $sidebar, 'orphaned_widgets' ) ) ) { 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 || ( isset( $wp_registered_widgets[ $widget ]['id'] ) && $widget_id === $wp_registered_widgets[ $widget ]['id'] ) ) { return $sidebar; } } } } } } return false;}Changelog
Introduced in 2.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/widgets.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.