wp_assign_widget_to_sidebar( string $widget_id, string $sidebar_id )
Assigns a widget to the given sidebar.
Parameters
- $widget_id
-
(string) (Required) The widget id to assign.
- $sidebar_id
-
(string) (Required) The sidebar id to assign to. If empty, the widget won't be added to any sidebar.
Source
File: wp-includes/widgets.php
function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) { $sidebars = wp_get_sidebars_widgets(); foreach ( $sidebars as $maybe_sidebar_id => $widgets ) { foreach ( $widgets as $i => $maybe_widget_id ) { if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) { unset( $sidebars[ $maybe_sidebar_id ][ $i ] ); // We could technically break 2 here, but continue looping in case the id is duplicated. continue 2; } } } if ( $sidebar_id ) { $sidebars[ $sidebar_id ][] = $widget_id; } wp_set_sidebars_widgets( $sidebars ); }
Changelog
Version | Description |
---|---|
5.8.0 | Introduced. |
© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_assign_widget_to_sidebar