wp_get_nav_menu_to_edit( int $menu_id = 0 ): string|WP_Error|null
- Since
- 3.0.0
- Source
wp-admin/includes/nav-menu.php:1261
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
Return value
string|WP_Error|null- The menu formatted to edit or error object on failure.
Null if the$menu_idparameter is not supplied or the term does not exist.
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
- Scaling
- Scales with input
- Instructions
- 14–87
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_term().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.
Third-party callbacks on 'wp_edit_nav_menu_walker' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_nav_menu() | 14 | wp_get_nav_menu_object(), is_nav_menu(), is_wp_error() |
is_nav_menu() && empty($menu_items) | 32–33 | wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __() |
is_nav_menu() && !empty($menu_items) && !$walker_class_name | 52–53 | wp_get_nav_menu_object(), is_nav_menu(), wp_get_nav_menu_items(), __(), apply_filters(), __(), sprintf() |
is_nav_menu() && !empty($menu_items) && $walker_class_name | 65–67 | wp_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_name | 75–77 | wp_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_name | 85–87 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 120 | 14–87 | 12 | |
| 8.5 | 120 | 14–87 | 12 | |
| 8.4 | 120 | 14–87 | 12 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 122 | 14–89 | 12 | |
| 8.2 | 122 | 14–89 | 12 | |
| 8.1 | 122 | 14–89 | 12 | |
| 7.4 | 122 | 14–89 | 12 |
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:
- apply_filters( wp_edit_nav_menu_walker )filterline 1284 (+23 into the body)
Filters the Walker class used when adding nav menu items.
Uses · 9
- wp_get_nav_menu_object()Returns a navigation menu object.
- is_nav_menu()Determines whether the given ID is a navigation menu.
- wp_get_nav_menu_items()Retrieves all menu items of a navigation menu.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_admin_notice()Creates and returns the markup for an admin notice.
- walk_nav_menu_tree()Retrieves the HTML list content for nav menu items.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- WP_Error::__construct()Initializes the error.
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.
Signature, return type and hooks compared across 5 parsed releases.
string|WP_Error to string|WP_Error|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.