WP_REST_Sidebars_Controller::update_item( WP_REST_Request $request ): WP_REST_Response

In this article

Updates a sidebar.

Parameters

$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response Response object on success, or WP_Error object on failure.

Source

public function update_item( $request ) {
	if ( isset( $request['widgets'] ) ) {
		$sidebars = wp_get_sidebars_widgets();

		foreach ( $sidebars as $sidebar_id => $widgets ) {
			foreach ( $widgets as $i => $widget_id ) {
				// This automatically removes the passed widget IDs from any other sidebars in use.
				if ( $sidebar_id !== $request['id'] && in_array( $widget_id, $request['widgets'], true ) ) {
					unset( $sidebars[ $sidebar_id ][ $i ] );
				}

				// This automatically removes omitted widget IDs to the inactive sidebar.
				if ( $sidebar_id === $request['id'] && ! in_array( $widget_id, $request['widgets'], true ) ) {
					$sidebars['wp_inactive_widgets'][] = $widget_id;
				}
			}
		}

		$sidebars[ $request['id'] ] = $request['widgets'];

		wp_set_sidebars_widgets( $sidebars );
	}

	$request['context'] = 'edit';

	$sidebar = $this->get_sidebar( $request['id'] );

	/**
	 * Fires after a sidebar is updated via the REST API.
	 *
	 * @since 5.8.0
	 *
	 * @param array           $sidebar The updated sidebar.
	 * @param WP_REST_Request $request Request object.
	 */
	do_action( 'rest_save_sidebar', $sidebar, $request );

	return $this->prepare_item_for_response( $sidebar, $request );
}

Hooks

do_action( ‘rest_save_sidebar’, array $sidebar, WP_REST_Request $request )

Fires after a sidebar is updated via the REST API.

Changelog

VersionDescription
5.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.