wppaste
WordPress

wp_get_nav_menu_to_edit( int $menu_id = 0 ): string|WP_Error

Since
3.0.0
Source
wp-admin/includes/nav-menu.php:1244
Returns the menu formatted to edit.

Compatibility

WordPress
since 3.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$menu_idintoptional
The ID of the menu to format. Default 0.Default: 0

Return value

string|WP_Error
The menu formatted to edit or error object on failure.

Performance profile

How much work a call to wp_get_nav_menu_to_edit() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_term().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–87

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.

Plugin surface
1 hook

Third-party callbacks on 'wp_edit_nav_menu_walker' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_term()one call below wp_get_nav_menu_to_edit()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_nav_menu_to_edit() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_nav_menu()14wp_get_nav_menu_object(), is_nav_menu(), is_wp_error()
is_nav_menu() && empty($menu_items)32–33wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __()
is_nav_menu() && !empty($menu_items) && !$walker_class_name52–53wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __(), apply_filters(), __(), sprintf()
is_nav_menu() && !empty($menu_items) && $walker_class_name65–67wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __(), apply_filters(), array_map(), walker()
is_nav_menu() && !empty($menu_items) && $walker_class_name75–77wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __(), apply_filters(), __(), wp_get_admin_notice(), array_map(), walker()
is_nav_menu() && !empty($menu_items) && $walker_class_name85–87wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __(), apply_filters(), __(), wp_get_admin_notice(), __(), wp_get_admin_notice(), array_map(), walker()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12014–8712
8.512014–8712
8.412014–87122 fewer instructions than PHP 8.3
8.312214–8912
8.212214–8912
8.112214–8912
7.412214–8912

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_get_nav_menu_to_edit() runs, in this order:

  1. apply_filters( wp_edit_nav_menu_walker )filterline 1267 (+23 into the body)

    Filters the Walker class used when adding nav menu items.

Uses · 9

Source code

function wp_get_nav_menu_to_edit( $menu_id = 0 ) {	$menu = wp_get_nav_menu_object( $menu_id ); 	// If the menu exists, get its items.	if ( is_nav_menu( $menu ) ) {		$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) );		$result     = '<div id="menu-instructions" class="post-body-plain';		$result    .= ( ! empty( $menu_items ) ) ? ' menu-instructions-inactive">' : '">';		$result    .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';		$result    .= '</div>'; 		if ( empty( $menu_items ) ) {			return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';		} 		/**		 * Filters the Walker class used when adding nav menu items.		 *		 * @since 3.0.0		 *		 * @param string $class   The walker class to use. Default 'Walker_Nav_Menu_Edit'.		 * @param int    $menu_id ID of the menu being rendered.		 */		$walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id ); 		if ( class_exists( $walker_class_name ) ) {			$walker = new $walker_class_name();		} else {			return new WP_Error(				'menu_walker_not_exist',				sprintf(					/* translators: %s: Walker class name. */					__( 'The Walker class named %s does not exist.' ),					'<strong>' . $walker_class_name . '</strong>'				)			);		} 		$some_pending_menu_items = false;		$some_invalid_menu_items = false; 		foreach ( (array) $menu_items as $menu_item ) {			if ( isset( $menu_item->post_status ) && 'draft' === $menu_item->post_status ) {				$some_pending_menu_items = true;			}			if ( ! empty( $menu_item->_invalid ) ) {				$some_invalid_menu_items = true;			}		} 		if ( $some_pending_menu_items ) {			$message     = __( 'Click Save Menu to make pending menu items public.' );			$notice_args = array(				'type'               => 'info',				'additional_classes' => array( 'notice-alt', 'inline' ),			);			$result     .= wp_get_admin_notice( $message, $notice_args );		} 		if ( $some_invalid_menu_items ) {			$message     = __( 'There are some invalid menu items. Please check or delete them.' );			$notice_args = array(				'type'               => 'error',				'additional_classes' => array( 'notice-alt', 'inline' ),			);			$result     .= wp_get_admin_notice( $message, $notice_args );		} 		$result .= '<ul class="menu" id="menu-to-edit"> ';		$result .= walk_nav_menu_tree(			array_map( 'wp_setup_nav_menu_item', $menu_items ),			0,			(object) array( 'walker' => $walker )		);		$result .= ' </ul> '; 		return $result;	} elseif ( is_wp_error( $menu ) ) {		return $menu;	}

Changelog

Introduced in 3.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Return type changed from string|WP_Error to string|WP_Error|null.verified against source
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/nav-menu.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.