5 * This API is used for creating dynamic sidebar without hardcoding functionality into
8 * Includes both internal WordPress routines and theme-use routines.
10 * This functionality was found in a plugin before the WordPress 2.2 release, which
11 * included it in the core from that point on.
13 * @link https://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets
14 * @link https://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API
26 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
29 * Stores the sidebars, since many themes can have more than one.
31 * @global array $wp_registered_sidebars
34 $wp_registered_sidebars = array();
37 * Stores the registered widgets.
39 * @global array $wp_registered_widgets
42 $wp_registered_widgets = array();
45 * Stores the registered widget control (options).
47 * @global array $wp_registered_widget_controls
50 $wp_registered_widget_controls = array();
52 * @global array $wp_registered_widget_updates
54 $wp_registered_widget_updates = array();
59 * @global array $_wp_sidebars_widgets
61 $_wp_sidebars_widgets = array();
66 * @global array $_wp_deprecated_widgets_callbacks
68 $GLOBALS['_wp_deprecated_widgets_callbacks'] = array(
70 'wp_widget_pages_control',
72 'wp_widget_calendar_control',
74 'wp_widget_archives_control',
77 'wp_widget_meta_control',
79 'wp_widget_recent_entries',
80 'wp_widget_recent_entries_control',
81 'wp_widget_tag_cloud',
82 'wp_widget_tag_cloud_control',
83 'wp_widget_categories',
84 'wp_widget_categories_control',
86 'wp_widget_text_control',
88 'wp_widget_rss_control',
89 'wp_widget_recent_comments',
90 'wp_widget_recent_comments_control'
94 // Template tags & API functions
100 * Registers a WP_Widget widget
106 * @global WP_Widget_Factory $wp_widget_factory
108 * @param string $widget_class The name of a class that extends WP_Widget
110 function register_widget($widget_class) {
111 global $wp_widget_factory;
113 $wp_widget_factory->register($widget_class);
117 * Unregister a widget
119 * Unregisters a WP_Widget widget. Useful for unregistering default widgets.
120 * Run within a function hooked to the widgets_init action.
126 * @global WP_Widget_Factory $wp_widget_factory
128 * @param string $widget_class The name of a class that extends WP_Widget
130 function unregister_widget($widget_class) {
131 global $wp_widget_factory;
133 $wp_widget_factory->unregister($widget_class);
137 * Creates multiple sidebars.
139 * If you wanted to quickly create multiple sidebars for a theme or internally.
140 * This function will allow you to do so. If you don't pass the 'name' and/or
141 * 'id' in `$args`, then they will be built for you.
145 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
147 * @global array $wp_registered_sidebars
149 * @param int $number Optional. Number of sidebars to create. Default 1.
150 * @param array|string $args {
151 * Optional. Array or string of arguments for building a sidebar.
153 * @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple
154 * sidebars are being defined, the id will have "-2" appended, and so on.
155 * Default 'sidebar-' followed by the number the sidebar creation is currently at.
156 * @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering
157 * more than one sidebar, include '%d' in the string as a placeholder for the uniquely
158 * assigned number for each sidebar.
159 * Default 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'.
162 function register_sidebars( $number = 1, $args = array() ) {
163 global $wp_registered_sidebars;
164 $number = (int) $number;
166 if ( is_string($args) )
167 parse_str($args, $args);
169 for ( $i = 1; $i <= $number; $i++ ) {
173 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
175 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
177 // Custom specified ID's are suffixed if they exist already.
178 // Automatically generated sidebar names need to be suffixed regardless starting at -0
179 if ( isset($args['id']) ) {
180 $_args['id'] = $args['id'];
181 $n = 2; // Start at -2 for conflicting custom ID's
182 while ( is_registered_sidebar( $_args['id'] ) ) {
183 $_args['id'] = $args['id'] . '-' . $n++;
186 $n = count( $wp_registered_sidebars );
188 $_args['id'] = 'sidebar-' . ++$n;
189 } while ( is_registered_sidebar( $_args['id'] ) );
191 register_sidebar($_args);
196 * Builds the definition for a single sidebar and returns the ID.
198 * Accepts either a string or an array and then parses that against a set
199 * of default arguments for the new sidebar. WordPress will automatically
200 * generate a sidebar ID and name based on the current number of registered
201 * sidebars if those arguments are not included.
203 * When allowing for automatic generation of the name and ID parameters, keep
204 * in mind that the incrementor for your sidebar can change over time depending
205 * on what other plugins and themes are installed.
207 * If theme support for 'widgets' has not yet been added when this function is
208 * called, it will be automatically enabled through the use of add_theme_support()
212 * @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
214 * @param array|string $args {
215 * Optional. Array or string of arguments for the sidebar being registered.
217 * @type string $name The name or title of the sidebar displayed in the Widgets
218 * interface. Default 'Sidebar $instance'.
219 * @type string $id The unique identifier by which the sidebar will be called.
220 * Default 'sidebar-$instance'.
221 * @type string $description Description of the sidebar, displayed in the Widgets interface.
222 * Default empty string.
223 * @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
225 * @type string $before_widget HTML content to prepend to each widget's HTML output when
226 * assigned to this sidebar. Default is an opening list item element.
227 * @type string $after_widget HTML content to append to each widget's HTML output when
228 * assigned to this sidebar. Default is a closing list item element.
229 * @type string $before_title HTML content to prepend to the sidebar title when displayed.
230 * Default is an opening h2 element.
231 * @type string $after_title HTML content to append to the sidebar title when displayed.
232 * Default is a closing h2 element.
234 * @return string Sidebar ID added to $wp_registered_sidebars global.
236 function register_sidebar($args = array()) {
237 global $wp_registered_sidebars;
239 $i = count($wp_registered_sidebars) + 1;
241 $id_is_empty = empty( $args['id'] );
244 'name' => sprintf(__('Sidebar %d'), $i ),
245 'id' => "sidebar-$i",
248 'before_widget' => '<li id="%1$s" class="widget %2$s">',
249 'after_widget' => "</li>\n",
250 'before_title' => '<h2 class="widgettitle">',
251 'after_title' => "</h2>\n",
254 $sidebar = wp_parse_args( $args, $defaults );
256 if ( $id_is_empty ) {
257 /* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */
258 _doing_it_wrong( __FUNCTION__, sprintf( __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), '<code>id</code>', $sidebar['name'], $sidebar['id'] ), '4.2.0' );
261 $wp_registered_sidebars[$sidebar['id']] = $sidebar;
263 add_theme_support('widgets');
266 * Fires once a sidebar has been registered.
270 * @param array $sidebar Parsed arguments for the registered sidebar.
272 do_action( 'register_sidebar', $sidebar );
274 return $sidebar['id'];
278 * Removes a sidebar from the list.
282 * @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
284 * @param string $name The ID of the sidebar when it was added.
286 function unregister_sidebar( $name ) {
287 global $wp_registered_sidebars;
289 unset( $wp_registered_sidebars[ $name ] );
293 * Checks if a sidebar is registered.
297 * @global array $wp_registered_sidebars Registered sidebars.
299 * @param string|int $sidebar_id The ID of the sidebar when it was registered.
300 * @return bool True if the sidebar is registered, false otherwise.
302 function is_registered_sidebar( $sidebar_id ) {
303 global $wp_registered_sidebars;
305 return isset( $wp_registered_sidebars[ $sidebar_id ] );
309 * Register an instance of a widget.
311 * The default widget option is 'classname' that can be overridden.
313 * The function can also be used to un-register widgets when `$output_callback`
314 * parameter is an empty string.
318 * @global array $wp_registered_widgets Uses stored registered widgets.
319 * @global array $wp_register_widget_defaults Retrieves widget defaults.
320 * @global array $wp_registered_widget_updates
321 * @global array $_wp_deprecated_widgets_callbacks
323 * @param int|string $id Widget ID.
324 * @param string $name Widget display title.
325 * @param callable $output_callback Run when widget is called.
326 * @param array $options {
327 * Optional. An array of supplementary widget options for the instance.
329 * @type string $classname Class name for the widget's HTML container. Default is a shortened
330 * version of the output callback name.
331 * @type string $description Widget description for display in the widget administration
332 * panel and/or theme.
335 function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) {
336 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks;
338 $id = strtolower($id);
340 if ( empty($output_callback) ) {
341 unset($wp_registered_widgets[$id]);
345 $id_base = _get_widget_id_base($id);
346 if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) {
347 unset( $wp_registered_widget_controls[ $id ] );
348 unset( $wp_registered_widget_updates[ $id_base ] );
352 $defaults = array('classname' => $output_callback);
353 $options = wp_parse_args($options, $defaults);
357 'callback' => $output_callback,
358 'params' => array_slice(func_get_args(), 4)
360 $widget = array_merge($widget, $options);
362 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) {
365 * Fires once for each registered widget.
369 * @param array $widget An array of default widget arguments.
371 do_action( 'wp_register_sidebar_widget', $widget );
372 $wp_registered_widgets[$id] = $widget;
377 * Retrieve description for widget.
379 * When registering widgets, the options can also include 'description' that
380 * describes the widget for display on the widget administration panel or
385 * @global array $wp_registered_widgets
387 * @param int|string $id Widget ID.
388 * @return string|void Widget description, if available.
390 function wp_widget_description( $id ) {
391 if ( !is_scalar($id) )
394 global $wp_registered_widgets;
396 if ( isset($wp_registered_widgets[$id]['description']) )
397 return esc_html( $wp_registered_widgets[$id]['description'] );
401 * Retrieve description for a sidebar.
403 * When registering sidebars a 'description' parameter can be included that
404 * describes the sidebar for display on the widget administration panel.
408 * @global array $wp_registered_sidebars
410 * @param string $id sidebar ID.
411 * @return string|void Sidebar description, if available.
413 function wp_sidebar_description( $id ) {
414 if ( !is_scalar($id) )
417 global $wp_registered_sidebars;
419 if ( isset($wp_registered_sidebars[$id]['description']) )
420 return esc_html( $wp_registered_sidebars[$id]['description'] );
424 * Remove widget from sidebar.
428 * @param int|string $id Widget ID.
430 function wp_unregister_sidebar_widget($id) {
433 * Fires just before a widget is removed from a sidebar.
437 * @param int $id The widget ID.
439 do_action( 'wp_unregister_sidebar_widget', $id );
441 wp_register_sidebar_widget($id, '', '');
442 wp_unregister_widget_control($id);
446 * Registers widget control callback for customizing options.
448 * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
449 * option is never used. The 'width' option is the width of the fully expanded
450 * control form, but try hard to use the default width. The 'id_base' is for
451 * multi-widgets (widgets which allow multiple instances such as the text
452 * widget), an id_base must be provided. The widget id will end up looking like
453 * `{$id_base}-{$unique_number}`.
457 * @todo Document `$options` as a hash notation, re: WP_Widget::__construct() cross-reference.
458 * @todo `$params` parameter?
460 * @global array $wp_registered_widget_controls
461 * @global array $wp_registered_widget_updates
462 * @global array $wp_registered_widgets
463 * @global array $_wp_deprecated_widgets_callbacks
465 * @param int|string $id Sidebar ID.
466 * @param string $name Sidebar display name.
467 * @param callable $control_callback Run when sidebar is displayed.
468 * @param array|string $options Optional. Widget options. See description above. Default empty array.
470 function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) {
471 global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
473 $id = strtolower($id);
474 $id_base = _get_widget_id_base($id);
476 if ( empty($control_callback) ) {
477 unset($wp_registered_widget_controls[$id]);
478 unset($wp_registered_widget_updates[$id_base]);
482 if ( in_array($control_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($control_callback) ) {
483 unset( $wp_registered_widgets[ $id ] );
487 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
490 $defaults = array('width' => 250, 'height' => 200 ); // height is never used
491 $options = wp_parse_args($options, $defaults);
492 $options['width'] = (int) $options['width'];
493 $options['height'] = (int) $options['height'];
498 'callback' => $control_callback,
499 'params' => array_slice(func_get_args(), 4)
501 $widget = array_merge($widget, $options);
503 $wp_registered_widget_controls[$id] = $widget;
505 if ( isset($wp_registered_widget_updates[$id_base]) )
508 if ( isset($widget['params'][0]['number']) )
509 $widget['params'][0]['number'] = -1;
511 unset($widget['width'], $widget['height'], $widget['name'], $widget['id']);
512 $wp_registered_widget_updates[$id_base] = $widget;
516 * @global array $wp_registered_widget_updates
518 * @param string $id_base
519 * @param callable $update_callback
520 * @param array $options
522 function _register_widget_update_callback($id_base, $update_callback, $options = array()) {
523 global $wp_registered_widget_updates;
525 if ( isset($wp_registered_widget_updates[$id_base]) ) {
526 if ( empty($update_callback) )
527 unset($wp_registered_widget_updates[$id_base]);
532 'callback' => $update_callback,
533 'params' => array_slice(func_get_args(), 3)
536 $widget = array_merge($widget, $options);
537 $wp_registered_widget_updates[$id_base] = $widget;
542 * @global array $wp_registered_widget_controls
544 * @param int|string $id
545 * @param string $name
546 * @param callable $form_callback
547 * @param array $options
549 function _register_widget_form_callback($id, $name, $form_callback, $options = array()) {
550 global $wp_registered_widget_controls;
552 $id = strtolower($id);
554 if ( empty($form_callback) ) {
555 unset($wp_registered_widget_controls[$id]);
559 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
562 $defaults = array('width' => 250, 'height' => 200 );
563 $options = wp_parse_args($options, $defaults);
564 $options['width'] = (int) $options['width'];
565 $options['height'] = (int) $options['height'];
570 'callback' => $form_callback,
571 'params' => array_slice(func_get_args(), 4)
573 $widget = array_merge($widget, $options);
575 $wp_registered_widget_controls[$id] = $widget;
579 * Remove control callback for widget.
583 * @param int|string $id Widget ID.
585 function wp_unregister_widget_control($id) {
586 wp_register_widget_control( $id, '', '' );
590 * Display dynamic sidebar.
592 * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or
593 * 'name' parameter for its registered sidebars you can pass an id or name as the $index parameter.
594 * Otherwise, you can pass in a numerical index to display the sidebar at that index.
598 * @global array $wp_registered_sidebars
599 * @global array $wp_registered_widgets
601 * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar.
602 * @return bool True, if widget sidebar was found and called. False if not found or not called.
604 function dynamic_sidebar( $index = 1 ) {
605 global $wp_registered_sidebars, $wp_registered_widgets;
607 if ( is_int( $index ) ) {
608 $index = "sidebar-$index";
610 $index = sanitize_title( $index );
611 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
612 if ( sanitize_title( $value['name'] ) == $index ) {
619 $sidebars_widgets = wp_get_sidebars_widgets();
620 if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {
621 /** This action is documented in wp-includes/widget.php */
622 do_action( 'dynamic_sidebar_before', $index, false );
623 /** This action is documented in wp-includes/widget.php */
624 do_action( 'dynamic_sidebar_after', $index, false );
625 /** This filter is documented in wp-includes/widget.php */
626 return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
630 * Fires before widgets are rendered in a dynamic sidebar.
632 * Note: The action also fires for empty sidebars, and on both the front-end
633 * and back-end, including the Inactive Widgets sidebar on the Widgets screen.
637 * @param int|string $index Index, name, or ID of the dynamic sidebar.
638 * @param bool $has_widgets Whether the sidebar is populated with widgets.
641 do_action( 'dynamic_sidebar_before', $index, true );
642 $sidebar = $wp_registered_sidebars[$index];
645 foreach ( (array) $sidebars_widgets[$index] as $id ) {
647 if ( !isset($wp_registered_widgets[$id]) ) continue;
649 $params = array_merge(
650 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
651 (array) $wp_registered_widgets[$id]['params']
654 // Substitute HTML id and class attributes into before_widget
656 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
657 if ( is_string($cn) )
658 $classname_ .= '_' . $cn;
659 elseif ( is_object($cn) )
660 $classname_ .= '_' . get_class($cn);
662 $classname_ = ltrim($classname_, '_');
663 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
666 * Filter the parameters passed to a widget's display callback.
668 * Note: The filter is evaluated on both the front-end and back-end,
669 * including for the Inactive Widgets sidebar on the Widgets screen.
673 * @see register_sidebar()
675 * @param array $params {
676 * @type array $args {
677 * An array of widget display arguments.
679 * @type string $name Name of the sidebar the widget is assigned to.
680 * @type string $id ID of the sidebar the widget is assigned to.
681 * @type string $description The sidebar description.
682 * @type string $class CSS class applied to the sidebar container.
683 * @type string $before_widget HTML markup to prepend to each widget in the sidebar.
684 * @type string $after_widget HTML markup to append to each widget in the sidebar.
685 * @type string $before_title HTML markup to prepend to the widget title when displayed.
686 * @type string $after_title HTML markup to append to the widget title when displayed.
687 * @type string $widget_id ID of the widget.
688 * @type string $widget_name Name of the widget.
690 * @type array $widget_args {
691 * An array of multi-widget arguments.
693 * @type int $number Number increment used for multiples of the same widget.
697 $params = apply_filters( 'dynamic_sidebar_params', $params );
699 $callback = $wp_registered_widgets[$id]['callback'];
702 * Fires before a widget's display callback is called.
704 * Note: The action fires on both the front-end and back-end, including
705 * for widgets in the Inactive Widgets sidebar on the Widgets screen.
707 * The action is not fired for empty sidebars.
711 * @param array $widget_id {
712 * An associative array of widget arguments.
714 * @type string $name Name of the widget.
715 * @type string $id Widget ID.
716 * @type array|callable $callback When the hook is fired on the front-end, $callback is an array
717 * containing the widget object. Fired on the back-end, $callback
718 * is 'wp_widget_control', see $_callback.
719 * @type array $params An associative array of multi-widget arguments.
720 * @type string $classname CSS class applied to the widget container.
721 * @type string $description The widget description.
722 * @type array $_callback When the hook is fired on the back-end, $_callback is populated
723 * with an array containing the widget object, see $callback.
726 do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] );
728 if ( is_callable($callback) ) {
729 call_user_func_array($callback, $params);
735 * Fires after widgets are rendered in a dynamic sidebar.
737 * Note: The action also fires for empty sidebars, and on both the front-end
738 * and back-end, including the Inactive Widgets sidebar on the Widgets screen.
742 * @param int|string $index Index, name, or ID of the dynamic sidebar.
743 * @param bool $has_widgets Whether the sidebar is populated with widgets.
746 do_action( 'dynamic_sidebar_after', $index, true );
749 * Filter whether a sidebar has widgets.
751 * Note: The filter is also evaluated for empty sidebars, and on both the front-end
752 * and back-end, including the Inactive Widgets sidebar on the Widgets screen.
756 * @param bool $did_one Whether at least one widget was rendered in the sidebar.
758 * @param int|string $index Index, name, or ID of the dynamic sidebar.
760 return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index );
764 * Whether widget is displayed on the front-end.
766 * Either $callback or $id_base can be used
767 * $id_base is the first argument when extending WP_Widget class
768 * Without the optional $widget_id parameter, returns the ID of the first sidebar
769 * in which the first instance of the widget with the given callback or $id_base is found.
770 * With the $widget_id parameter, returns the ID of the sidebar where
771 * the widget with that callback/$id_base AND that ID is found.
773 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
774 * this function has to run after widgets have initialized, at action 'init' or later.
778 * @global array $wp_registered_widgets
780 * @param string $callback Optional, Widget callback to check.
781 * @param int $widget_id Optional, but needed for checking. Widget ID.
782 * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget.
783 * @param bool $skip_inactive Optional, whether to check in 'wp_inactive_widgets'.
784 * @return string|false False if widget is not active or id of sidebar in which the widget is active.
786 function is_active_widget($callback = false, $widget_id = false, $id_base = false, $skip_inactive = true) {
787 global $wp_registered_widgets;
789 $sidebars_widgets = wp_get_sidebars_widgets();
791 if ( is_array($sidebars_widgets) ) {
792 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
793 if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
797 if ( is_array($widgets) ) {
798 foreach ( $widgets as $widget ) {
799 if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) {
800 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
811 * Whether the dynamic sidebar is enabled and used by theme.
815 * @global array $wp_registered_widgets
816 * @global array $wp_registered_sidebars
818 * @return bool True, if using widgets. False, if not using widgets.
820 function is_dynamic_sidebar() {
821 global $wp_registered_widgets, $wp_registered_sidebars;
822 $sidebars_widgets = get_option('sidebars_widgets');
823 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
824 if ( count($sidebars_widgets[$index]) ) {
825 foreach ( (array) $sidebars_widgets[$index] as $widget )
826 if ( array_key_exists($widget, $wp_registered_widgets) )
834 * Whether a sidebar is in use.
838 * @param string|int $index Sidebar name, id or number to check.
839 * @return bool true if the sidebar is in use, false otherwise.
841 function is_active_sidebar( $index ) {
842 $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
843 $sidebars_widgets = wp_get_sidebars_widgets();
844 $is_active_sidebar = ! empty( $sidebars_widgets[$index] );
847 * Filter whether a dynamic sidebar is considered "active".
851 * @param bool $is_active_sidebar Whether or not the sidebar should be considered "active".
852 * In other words, whether the sidebar contains any widgets.
853 * @param int|string $index Index, name, or ID of the dynamic sidebar.
855 return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index );
859 // Internal Functions
863 * Retrieve full list of sidebars and their widget instance IDs.
865 * Will upgrade sidebar widget list, if needed. Will also save updated list, if
871 * @global array $_wp_sidebars_widgets
872 * @global array $sidebars_widgets
874 * @param bool $deprecated Not used (argument deprecated).
875 * @return array Upgraded list of widgets to version 3 array format when called from the admin.
877 function wp_get_sidebars_widgets( $deprecated = true ) {
878 if ( $deprecated !== true )
879 _deprecated_argument( __FUNCTION__, '2.8.1' );
881 global $_wp_sidebars_widgets, $sidebars_widgets;
883 // If loading from front page, consult $_wp_sidebars_widgets rather than options
884 // to see if wp_convert_widget_settings() has made manipulations in memory.
886 if ( empty($_wp_sidebars_widgets) )
887 $_wp_sidebars_widgets = get_option('sidebars_widgets', array());
889 $sidebars_widgets = $_wp_sidebars_widgets;
891 $sidebars_widgets = get_option('sidebars_widgets', array());
894 if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) )
895 unset($sidebars_widgets['array_version']);
898 * Filter the list of sidebars and their widgets.
902 * @param array $sidebars_widgets An associative array of sidebars and their widgets.
904 return apply_filters( 'sidebars_widgets', $sidebars_widgets );
908 * Set the sidebar widget option to update sidebars.
913 * @param array $sidebars_widgets Sidebar widgets and their settings.
915 function wp_set_sidebars_widgets( $sidebars_widgets ) {
916 if ( !isset( $sidebars_widgets['array_version'] ) )
917 $sidebars_widgets['array_version'] = 3;
918 update_option( 'sidebars_widgets', $sidebars_widgets );
922 * Retrieve default registered sidebars list.
927 * @global array $wp_registered_sidebars
931 function wp_get_widget_defaults() {
932 global $wp_registered_sidebars;
936 foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
937 $defaults[$index] = array();
943 * Convert the widget settings from single to multi-widget format.
947 * @global array $_wp_sidebars_widgets
949 * @param string $base_name
950 * @param string $option_name
951 * @param array $settings
954 function wp_convert_widget_settings($base_name, $option_name, $settings) {
955 // This test may need expanding.
956 $single = $changed = false;
957 if ( empty($settings) ) {
960 foreach ( array_keys($settings) as $number ) {
961 if ( 'number' == $number )
963 if ( !is_numeric($number) ) {
971 $settings = array( 2 => $settings );
973 // If loading from the front page, update sidebar in memory but don't save to options
975 $sidebars_widgets = get_option('sidebars_widgets');
977 if ( empty($GLOBALS['_wp_sidebars_widgets']) )
978 $GLOBALS['_wp_sidebars_widgets'] = get_option('sidebars_widgets', array());
979 $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];
982 foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
983 if ( is_array($sidebar) ) {
984 foreach ( $sidebar as $i => $name ) {
985 if ( $base_name == $name ) {
986 $sidebars_widgets[$index][$i] = "$name-2";
994 if ( is_admin() && $changed )
995 update_option('sidebars_widgets', $sidebars_widgets);
998 $settings['_multiwidget'] = 1;
1000 update_option( $option_name, $settings );
1006 * Output an arbitrary widget as a template tag.
1010 * @global WP_Widget_Factory $wp_widget_factory
1012 * @param string $widget The widget's PHP class name (see class-wp-widget.php).
1013 * @param array $instance Optional. The widget's instance settings. Default empty array.
1014 * @param array $args {
1015 * Optional. Array of arguments to configure the display of the widget.
1017 * @type string $before_widget HTML content that will be prepended to the widget's HTML output.
1018 * Default `<div class="widget %s">`, where `%s` is the widget's class name.
1019 * @type string $after_widget HTML content that will be appended to the widget's HTML output.
1021 * @type string $before_title HTML content that will be prepended to the widget's title when displayed.
1022 * Default `<h2 class="widgettitle">`.
1023 * @type string $after_title HTML content that will be appended to the widget's title when displayed.
1027 function the_widget( $widget, $instance = array(), $args = array() ) {
1028 global $wp_widget_factory;
1030 $widget_obj = $wp_widget_factory->widgets[$widget];
1031 if ( ! ( $widget_obj instanceof WP_Widget ) ) {
1035 $default_args = array(
1036 'before_widget' => '<div class="widget %s">',
1037 'after_widget' => "</div>",
1038 'before_title' => '<h2 class="widgettitle">',
1039 'after_title' => '</h2>',
1041 $args = wp_parse_args( $args, $default_args );
1042 $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );
1044 $instance = wp_parse_args($instance);
1047 * Fires before rendering the requested widget.
1051 * @param string $widget The widget's class name.
1052 * @param array $instance The current widget instance's settings.
1053 * @param array $args An array of the widget's sidebar arguments.
1055 do_action( 'the_widget', $widget, $instance, $args );
1057 $widget_obj->_set(-1);
1058 $widget_obj->widget($args, $instance);
1066 function _get_widget_id_base($id) {
1067 return preg_replace( '/-[0-9]+$/', '', $id );
1071 * Handle sidebars config after theme change
1076 * @global array $sidebars_widgets
1078 function _wp_sidebars_changed() {
1079 global $sidebars_widgets;
1081 if ( ! is_array( $sidebars_widgets ) )
1082 $sidebars_widgets = wp_get_sidebars_widgets();
1084 retrieve_widgets(true);
1088 * Look for "lost" widgets, this has to run at least on each theme change.
1092 * @global array $wp_registered_sidebars
1093 * @global array $sidebars_widgets
1094 * @global array $wp_registered_widgets
1096 * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value
1097 * of 'customize' defers updates for the Customizer.
1098 * @return array|void
1100 function retrieve_widgets( $theme_changed = false ) {
1101 global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
1103 $registered_sidebar_keys = array_keys( $wp_registered_sidebars );
1106 $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
1107 if ( is_array( $old_sidebars_widgets ) ) {
1108 // time() that sidebars were stored is in $old_sidebars_widgets['time']
1109 $_sidebars_widgets = $old_sidebars_widgets['data'];
1111 if ( 'customize' !== $theme_changed ) {
1112 remove_theme_mod( 'sidebars_widgets' );
1115 foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
1116 if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
1120 if ( !in_array( $sidebar, $registered_sidebar_keys ) ) {
1121 $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $widgets;
1122 unset( $_sidebars_widgets[$sidebar] );
1126 if ( empty( $sidebars_widgets ) )
1129 unset( $sidebars_widgets['array_version'] );
1131 $old = array_keys($sidebars_widgets);
1133 sort($registered_sidebar_keys);
1135 if ( $old == $registered_sidebar_keys )
1138 $_sidebars_widgets = array(
1139 'wp_inactive_widgets' => !empty( $sidebars_widgets['wp_inactive_widgets'] ) ? $sidebars_widgets['wp_inactive_widgets'] : array()
1142 unset( $sidebars_widgets['wp_inactive_widgets'] );
1144 foreach ( $wp_registered_sidebars as $id => $settings ) {
1145 if ( $theme_changed ) {
1146 $_sidebars_widgets[$id] = array_shift( $sidebars_widgets );
1148 // no theme change, grab only sidebars that are currently registered
1149 if ( isset( $sidebars_widgets[$id] ) ) {
1150 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
1151 unset( $sidebars_widgets[$id] );
1156 foreach ( $sidebars_widgets as $val ) {
1157 if ( is_array($val) && ! empty( $val ) )
1158 $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val;
1162 // discard invalid, theme-specific widgets from sidebars
1163 $shown_widgets = array();
1165 foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
1166 if ( !is_array($widgets) )
1169 $_widgets = array();
1170 foreach ( $widgets as $widget ) {
1171 if ( isset($wp_registered_widgets[$widget]) )
1172 $_widgets[] = $widget;
1175 $_sidebars_widgets[$sidebar] = $_widgets;
1176 $shown_widgets = array_merge($shown_widgets, $_widgets);
1179 $sidebars_widgets = $_sidebars_widgets;
1180 unset($_sidebars_widgets, $_widgets);
1182 // find hidden/lost multi-widget instances
1183 $lost_widgets = array();
1184 foreach ( $wp_registered_widgets as $key => $val ) {
1185 if ( in_array($key, $shown_widgets, true) )
1188 $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
1190 if ( 2 > (int) $number )
1193 $lost_widgets[] = $key;
1196 $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
1197 if ( 'customize' !== $theme_changed ) {
1198 wp_set_sidebars_widgets( $sidebars_widgets );
1201 return $sidebars_widgets;
1205 * Display the RSS entries in a list.
1209 * @param string|array|object $rss RSS url.
1210 * @param array $args Widget arguments.
1212 function wp_widget_rss_output( $rss, $args = array() ) {
1213 if ( is_string( $rss ) ) {
1214 $rss = fetch_feed($rss);
1215 } elseif ( is_array($rss) && isset($rss['url']) ) {
1217 $rss = fetch_feed($rss['url']);
1218 } elseif ( !is_object($rss) ) {
1222 if ( is_wp_error($rss) ) {
1223 if ( is_admin() || current_user_can('manage_options') )
1224 echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
1228 $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0, 'items' => 0 );
1229 $args = wp_parse_args( $args, $default_args );
1231 $items = (int) $args['items'];
1232 if ( $items < 1 || 20 < $items )
1234 $show_summary = (int) $args['show_summary'];
1235 $show_author = (int) $args['show_author'];
1236 $show_date = (int) $args['show_date'];
1238 if ( !$rss->get_item_quantity() ) {
1239 echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
1246 foreach ( $rss->get_items( 0, $items ) as $item ) {
1247 $link = $item->get_link();
1248 while ( stristr( $link, 'http' ) != $link ) {
1249 $link = substr( $link, 1 );
1251 $link = esc_url( strip_tags( $link ) );
1253 $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
1254 if ( empty( $title ) ) {
1255 $title = __( 'Untitled' );
1258 $desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
1259 $desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) );
1262 if ( $show_summary ) {
1265 // Change existing [...] to […].
1266 if ( '[...]' == substr( $summary, -5 ) ) {
1267 $summary = substr( $summary, 0, -5 ) . '[…]';
1270 $summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
1275 $date = $item->get_date( 'U' );
1278 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
1283 if ( $show_author ) {
1284 $author = $item->get_author();
1285 if ( is_object($author) ) {
1286 $author = $author->get_name();
1287 $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
1291 if ( $link == '' ) {
1292 echo "<li>$title{$date}{$summary}{$author}</li>";
1293 } elseif ( $show_summary ) {
1294 echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
1296 echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
1305 * Display RSS widget options form.
1307 * The options for what fields are displayed for the RSS form are all booleans
1308 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
1313 * @param array|string $args Values for input fields.
1314 * @param array $inputs Override default display options.
1316 function wp_widget_rss_form( $args, $inputs = null ) {
1317 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
1318 $inputs = wp_parse_args( $inputs, $default_inputs );
1320 $args['title'] = isset( $args['title'] ) ? $args['title'] : '';
1321 $args['url'] = isset( $args['url'] ) ? $args['url'] : '';
1322 $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
1324 if ( $args['items'] < 1 || 20 < $args['items'] ) {
1325 $args['items'] = 10;
1328 $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
1329 $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
1330 $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
1332 if ( ! empty( $args['error'] ) ) {
1333 echo '<p class="widget-error"><strong>' . sprintf( __( 'RSS Error: %s' ), $args['error'] ) . '</strong></p>';
1336 $esc_number = esc_attr( $args['number'] );
1337 if ( $inputs['url'] ) :
1339 <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
1340 <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p>
1341 <?php endif; if ( $inputs['title'] ) : ?>
1342 <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
1343 <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p>
1344 <?php endif; if ( $inputs['items'] ) : ?>
1345 <p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
1346 <select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
1348 for ( $i = 1; $i <= 20; ++$i ) {
1349 echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
1353 <?php endif; if ( $inputs['show_summary'] ) : ?>
1354 <p><input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
1355 <label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label></p>
1356 <?php endif; if ( $inputs['show_author'] ) : ?>
1357 <p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
1358 <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p>
1359 <?php endif; if ( $inputs['show_date'] ) : ?>
1360 <p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/>
1361 <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p>
1364 foreach ( array_keys($default_inputs) as $input ) :
1365 if ( 'hidden' === $inputs[$input] ) :
1366 $id = str_replace( '_', '-', $input );
1368 <input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" />
1375 * Process RSS feed widget data and optionally retrieve feed items.
1377 * The feed widget can not have more than 20 items or it will reset back to the
1378 * default, which is 10.
1380 * The resulting array has the feed title, feed url, feed link (from channel),
1381 * feed items, error (if any), and whether to show summary, author, and date.
1382 * All respectively in the order of the array elements.
1386 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
1387 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
1390 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
1391 $items = (int) $widget_rss['items'];
1392 if ( $items < 1 || 20 < $items )
1394 $url = esc_url_raw( strip_tags( $widget_rss['url'] ) );
1395 $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
1396 $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
1397 $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] :0;
1398 $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
1400 if ( $check_feed ) {
1401 $rss = fetch_feed($url);
1404 if ( is_wp_error($rss) ) {
1405 $error = $rss->get_error_message();
1407 $link = esc_url(strip_tags($rss->get_permalink()));
1408 while ( stristr($link, 'http') != $link )
1409 $link = substr($link, 1);
1416 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
1420 * Register all of the default WordPress widgets on startup.
1422 * Calls 'widgets_init' action after all of the WordPress widgets have been
1427 function wp_widgets_init() {
1428 if ( !is_blog_installed() )
1431 register_widget('WP_Widget_Pages');
1433 register_widget('WP_Widget_Calendar');
1435 register_widget('WP_Widget_Archives');
1437 if ( get_option( 'link_manager_enabled' ) )
1438 register_widget('WP_Widget_Links');
1440 register_widget('WP_Widget_Meta');
1442 register_widget('WP_Widget_Search');
1444 register_widget('WP_Widget_Text');
1446 register_widget('WP_Widget_Categories');
1448 register_widget('WP_Widget_Recent_Posts');
1450 register_widget('WP_Widget_Recent_Comments');
1452 register_widget('WP_Widget_RSS');
1454 register_widget('WP_Widget_Tag_Cloud');
1456 register_widget('WP_Nav_Menu_Widget');
1459 * Fires after all default WordPress widgets have been registered.
1463 do_action( 'widgets_init' );