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.
Return
(bool) True if the auto add setting was successfully updated.
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; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
5.9.0 | Introduced. |