wppaste
WordPress

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:914
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 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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–44

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 48.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()one call below is_active_widget()
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always11–38wp_get_sidebars_widgets()
is_array($sidebars_widgets) && !$sidebars_widgets && is_array($widgets) && !$widgets && $id_base === false29–44wp_get_sidebars_widgets(), _get_widget_id_base()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4811–4416
8.54811–4416
8.44811–44163 fewer instructions than PHP 8.3
8.35111–4716
8.25111–4716
8.15111–4716
7.45111–4716

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

Used by · 4

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 || $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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.