is_active_widget

Whether widget is displayed on the front end.

Description

(string|false) is_active_widget( (constant) $callback = false, (constant) $widget_id = false, (constant) $id_base = false, (bool) $skip_inactive = true ); 

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 or later.

Returns (string|false)

False if widget is not active or id of sidebar in which the widget is active.

Parameters (4)

0. $callback — Optional. (constant) => false
Optional, Widget callback to check. Default false.
1. $widget_id — Optional. (constant) => false
Widget ID. Optional, but needed for checking. Default false.
2. $id_base — Optional. (constant) => false
The base ID of a widget created by extending WP_Widget. Default false.
3. $skip_inactive — Optional. (bool) => true
Whether to check in wp_inactive_widgets.. Default true.

Usage

  1. if ( !function_exists( 'is_active_widget' ) ) { 
  2. require_once ABSPATH . WPINC . '/widgets.php'; 
  3.  
  4. // Optional, Widget callback to check. Default false. 
  5. $callback = false; 
  6.  
  7. // Optional. Widget ID. Optional, but needed for checking. Default false. 
  8. $widget_id = false; 
  9.  
  10. // Optional. The base ID of a widget created by extending WP_Widget. Default false. 
  11. $id_base = false; 
  12.  
  13. // Optional. Whether to check in 'wp_inactive_widgets'. Default true. 
  14. $skip_inactive = true; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = is_active_widget($callback, $widget_id, $id_base, $skip_inactive); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/widgets.php  
  1. function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) { 
  2. global $wp_registered_widgets; 
  3.  
  4. $sidebars_widgets = wp_get_sidebars_widgets(); 
  5.  
  6. if ( is_array($sidebars_widgets) ) { 
  7. foreach ( $sidebars_widgets as $sidebar => $widgets ) { 
  8. if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) { 
  9. continue; 
  10.  
  11. if ( is_array($widgets) ) { 
  12. foreach ( $widgets as $widget ) { 
  13. if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) { 
  14. if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] ) 
  15. return $sidebar; 
  16. return false;