wp_nav_menu( array $args = array() ): void|string|false
- Since
- 3.0.0, 4.7.0, 5.5.0
- Source
wp-includes/nav-menu-template.php:57
Compatibility
- WordPress
- since 5.5.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
$argsarrayoptional- Array of nav menu arguments.Default:
array()$menuint|string|WP_Termdefault: emptyDesired menu. Accepts a menu ID, slug, name, or object.$menu_classstringdefault: 'menu'CSS class to use for the ul element which forms the menu.$menu_idstringdefault: is the menu slug, incrementedThe ID that is applied to the ul element which forms the menu.$containerstringdefault: 'div'Whether to wrap the ul, and what to wrap it with.$container_classstringdefault: 'menu-{menu slug}-container'Class that is applied to the container.$container_idstringdefault: emptyThe ID that is applied to the container.$container_aria_labelstringdefault: emptyThe aria-label attribute that is applied to the container when it's a nav element.$fallback_cbcallable|falsedefault: is 'wp_page_menu'. Set to false for no fallbackIf the menu doesn't exist, a callback function will fire.$beforestringdefault: emptyText before the link markup.$afterstringdefault: emptyText after the link markup.$link_beforestringdefault: emptyText before the link text.$link_afterstringdefault: emptyText after the link text.$echobooldefault: trueWhether to echo the menu or return it.$depthintdefault: 0. Default 0How many levels of the hierarchy are to be included. 0 means all.$walkerobjectdefault: emptyInstance of a custom walker class.$theme_locationstringTheme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user.$items_wrapstringdefault: is a ul with an id and classHow the list items should be wrapped. Uses printf() format with numbered placeholders.$item_spacingstringdefault: 'preserve'Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.
Return value
void|string|false- Void if 'echo' argument is true, menu output if 'echo' is false.
False if there are no items or no menu was found.
Performance profile
How much work a call to wp_nav_menu() 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
- 43–202
- Plugin surface
- 7 hooks
- Called by
- 2
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 328.
Third-party callbacks on 'wp_nav_menu_args', 'pre_wp_nav_menu', 'wp_nav_menu_container_allowedtags' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_term()one call below wp_nav_menu() - optionoption read or write
get_option()one call below wp_nav_menu()
Further down the call graph this can also reach 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 · 17 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_nav_menu() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$nav_menu === null && !empty($args) | 43–45 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations() |
$nav_menu === null && !empty($args) && is_wp_error() | 47–49 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error() |
$nav_menu === null && !empty($args) && isset($args) && $args && !is_callable() | 50 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_callable() |
$nav_menu === null && !empty($args) && !is_wp_error() && !$args && empty($items) | 106–117 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
$nav_menu === null && !empty($args) && isset($args) && !is_callable() && !is_wp_error() && empty($items) | 113–122 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_callable(), is_wp_error(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $args && empty($items) | 114–154 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
$nav_menu === null && !empty($args) && isset($args) && $args && !is_callable() && !is_wp_error() && empty($items) | 121–155 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_callable(), is_wp_error(), apply_filters(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && !$args && !empty($items) | 130–146 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
$nav_menu === null && !empty($args) && isset($args) && !is_callable() && !is_wp_error() && !empty($items) | 137–151 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_callable(), is_wp_error(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $args && !empty($items) | 138–183 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
$nav_menu === null && !empty($args) && isset($args) && $args && !is_callable() && !is_wp_error() && !empty($items) | 145–184 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_callable(), is_wp_error(), apply_filters(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $allowed_tags && empty($items) | 146–161 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), esc_attr(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
5 further outcomes, up to 202 instructions
$nav_menu === null && !empty($args) && !is_wp_error() && $args && $allowed_tags && empty($items) | 153–168 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), esc_attr(), esc_attr(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $args && $allowed_tags && empty($items) | 162–173 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), esc_attr(), esc_attr(), esc_attr(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $allowed_tags && !empty($items) | 170–190 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), esc_attr(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $args && $allowed_tags && !empty($items) | 177–197 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), esc_attr(), esc_attr(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
$nav_menu === null && !empty($args) && !is_wp_error() && $args && $allowed_tags && !empty($items) | 186–202 | wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations(), is_wp_error(), apply_filters(), esc_attr(), esc_attr(), esc_attr(), _wp_menu_item_classes_by_context(), apply_filters(), walk_nav_menu_tree(), apply_filters(), apply_filters(), esc_attr(), esc_attr(), sprintf(), apply_filters() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 328 | 43–202 | 48 | |
| 8.5 | 328 | 43–202 | 48 | |
| 8.4 | 328 | 43–202 | 48 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 337 | 43–208 | 48 | |
| 8.2 | 337 | 43–208 | 48 | |
| 8.1 | 337 | 43–208 | 48 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 341 | 43–212 | 48 |
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 · 7
7 hooks fire while wp_nav_menu() runs, in this order:
- apply_filters( wp_nav_menu_args )filterline 97 (+40 into the body)
Filters the arguments used to display a navigation menu.
- apply_filters( pre_wp_nav_menu )filterline 113 (+56 into the body)
Filters whether to short-circuit the wp_nav_menu() output.
- apply_filters( wp_nav_menu_container_allowedtags )filterline 184 (+127 into the body)
Filters the list of HTML tags that are valid for use as menu containers.
- apply_filters( wp_nav_menu_objects )filterline 234 (+177 into the body)
Filters the sorted list of menu item objects before generating the menu's HTML.
- apply_filters( wp_nav_menu_items )filterline 267 (+210 into the body)
Filters the HTML list content for navigation menus.
- apply_filters( wp_nav_menu_{$menu->slug}_items )filterline 278 (+221 into the body)
Filters the HTML list content for a specific navigation menu.
- apply_filters( wp_nav_menu )filterline 302 (+245 into the body)
Filters the HTML content for navigation menus.
Uses · 10
- wp_parse_args()Merges user defined arguments into defaults array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_nav_menu_object()Returns a navigation menu object.
- get_nav_menu_locations()Retrieves all registered navigation menu locations and the menus assigned to them.
- wp_get_nav_menus()Returns all navigation menu objects.
- wp_get_nav_menu_items()Retrieves all menu items of a navigation menu.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- esc_attr()Escaping for HTML attributes.
- _wp_menu_item_classes_by_context()Adds the class property classes for the current context, if applicable.
- walk_nav_menu_tree()Retrieves the HTML list content for nav menu items.
Used by · 2
- WP_Customize_Nav_Menus::render_nav_menu_partial()Renders a specific menu via wp_nav_menu() using the supplied arguments.
- WP_Nav_Menu_Widget::widget()Outputs the content for the current Navigation Menu widget instance.
Source code
function wp_nav_menu( $args = array() ) { static $menu_id_slugs = array(); $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'container_aria_label' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'item_spacing' => 'preserve', 'depth' => 0, 'walker' => '', 'theme_location' => '', ); $args = wp_parse_args( $args, $defaults ); if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { // Invalid value, fall back to default. $args['item_spacing'] = $defaults['item_spacing']; } /** * Filters the arguments used to display a navigation menu. * * @since 3.0.0 * * @see wp_nav_menu() * * @param array $args Array of wp_nav_menu() arguments. */ $args = apply_filters( 'wp_nav_menu_args', $args ); $args = (object) $args; /** * Filters whether to short-circuit the wp_nav_menu() output. * * Returning a non-null value from the filter will short-circuit wp_nav_menu(), * echoing that value if $args->echo is true, returning that value otherwise. * * @since 3.9.0 * * @see wp_nav_menu() * * @param string|null $output Nav menu output to short-circuit with. Default null. * @param stdClass $args An object containing wp_nav_menu() arguments. */ $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args ); if ( null !== $nav_menu ) { if ( $args->echo ) { echo $nav_menu; return; } return $nav_menu; } // Get the nav menu based on the requested menu. $menu = wp_get_nav_menu_object( $args->menu ); // Get the nav menu based on the theme_location. $locations = get_nav_menu_locations(); if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) { $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); } // Get the first menu that has items if we still can't find a menu. if ( ! $menu && ! $args->theme_location ) { $menus = wp_get_nav_menus(); foreach ( $menus as $menu_maybe ) {Changelog
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
container_aria_label argument.from the docblockitem_spacing argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/nav-menu-template.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.