WP_REST_Menus_Controller::handle_auto_add() WordPress Method

The WP_REST_Menus_Controller::handle_auto_add() method is used to automatically add a new menu item to a given menu. This is useful for cases where a new menu item is created (e.g. a new post is published), and you want to automatically add it to an existing menu.

WP_REST_Menus_Controller::handle_auto_add( int $menu_id, WP_REST_Request $request ) #

Updates the menu’s auto add from a REST request.


Parameters

$menu_id

(int)(Required)The menu id to update.

$request

(WP_REST_Request)(Required)Full details about the request.


Top ↑

Return

(bool) True if the auto add setting was successfully updated.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php

	protected function handle_auto_add( $menu_id, $request ) {
		if ( ! isset( $request['auto_add'] ) ) {
			return true;
		}

		$nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );

		if ( ! isset( $nav_menu_option['auto_add'] ) ) {
			$nav_menu_option['auto_add'] = array();
		}

		$auto_add = $request['auto_add'];

		$i = array_search( $menu_id, $nav_menu_option['auto_add'], true );

		if ( $auto_add && false === $i ) {
			$nav_menu_option['auto_add'][] = $menu_id;
		} elseif ( ! $auto_add && false !== $i ) {
			array_splice( $nav_menu_option['auto_add'], $i, 1 );
		}

		$update = update_option( 'nav_menu_options', $nav_menu_option );

		/** This action is documented in wp-includes/nav-menu.php */
		do_action( 'wp_update_nav_menu', $menu_id );

		return $update;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.