wppaste
WordPress

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
Displays a navigation menu.

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: empty

    Desired 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, incremented

    The 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: empty

    The ID that is applied to the container.
  • $container_aria_labelstringdefault: empty

    The 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 fallback

    If the menu doesn't exist, a callback function will fire.
  • $beforestringdefault: empty

    Text before the link markup.
  • $afterstringdefault: empty

    Text after the link markup.
  • $link_beforestringdefault: empty

    Text before the link text.
  • $link_afterstringdefault: empty

    Text after the link text.
  • $echobooldefault: true

    Whether to echo the menu or return it.
  • $depthintdefault: 0. Default 0

    How many levels of the hierarchy are to be included. 0 means all.
  • $walkerobjectdefault: empty

    Instance of a custom walker class.
  • $theme_locationstring

    Theme 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 class

    How 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

Reaches the database via get_term().

Scaling
Scales with input

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

Instructions
43–202

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

Plugin surface
7 hooks

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.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_term()one call below wp_nav_menu()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
$nav_menu === null && !empty($args)43–45wp_parse_args(), apply_filters(), apply_filters(), wp_get_nav_menu_object(), get_nav_menu_locations()
$nav_menu === null && !empty($args) && is_wp_error()47–49wp_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()50wp_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–117wp_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–122wp_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–154wp_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–155wp_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–146wp_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–151wp_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–183wp_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–184wp_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–161wp_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–168wp_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–173wp_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–190wp_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–197wp_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–202wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev32843–20248
8.532843–20248
8.432843–202489 fewer instructions than PHP 8.3
8.333743–20848
8.233743–20848
8.133743–208484 fewer instructions than PHP 7.4
7.434143–21248

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:

  1. apply_filters( wp_nav_menu_args )filterline 97 (+40 into the body)

    Filters the arguments used to display a navigation menu.

  2. apply_filters( pre_wp_nav_menu )filterline 113 (+56 into the body)

    Filters whether to short-circuit the wp_nav_menu() output.

  3. 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.

  4. 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.

  5. apply_filters( wp_nav_menu_items )filterline 267 (+210 into the body)

    Filters the HTML list content for navigation menus.

  6. apply_filters( wp_nav_menu_{$menu->slug}_items )filterline 278 (+221 into the body)

    Filters the HTML list content for a specific navigation menu.

  7. apply_filters( wp_nav_menu )filterline 302 (+245 into the body)

    Filters the HTML content for navigation menus.

Uses · 10

Used by · 2

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.

  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.

5.5.0
Added the container_aria_label argument.from the docblock
4.7.0
Added the item_spacing argument.from the docblock
3.0.0
Introduced.from the docblock

About 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.