register_nav_menus()WP 3.0.0

Registers multiple custom navigation menus locations for a theme, to which the menus are then attached.

WP Menus are setup in admin-panel in the Appearence > Menus section and printed with wp_nav_menu() function.

When setting up a menu, you can add different links (items) there: categories, tags, pages, posts, external links, etc.

Registration of a custom menu for the admin panel

It is recommended to call this function on after_setup_theme hook.

This function automatically adds menu support for the theme, so there's no need to call add_theme_support('menus') if you are using it.

There are advanced menu settings on the Menus admin page in the Screen Options tab. It allows you to specify various attributes of the HTML links of the menu items.

No Hooks.

Return

null. Nothing (null).

Usage

register_nav_menus( $locations );
$locations(string[])
Associative array of menu location identifiers (like a slug) and descriptive text.
Default: array()

Examples

0

#1 An example of registering multiple menus

It also automatically adds menu support to the theme.

add_action( 'after_setup_theme', function(){
	register_nav_menus( [
		'header_menu' => 'Header menu',
		'footer_menu' => 'Footer menu'
	] );
} );

We will display these menus with wp_nav_menu().

Notes

  • Global. Array. $_wp_registered_nav_menus

Changelog

Since 3.0.0 Introduced.

register_nav_menus() code WP 6.4.3

function register_nav_menus( $locations = array() ) {
	global $_wp_registered_nav_menus;

	add_theme_support( 'menus' );

	foreach ( $locations as $key => $value ) {
		if ( is_int( $key ) ) {
			_doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3.0' );
			break;
		}
	}

	$_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
}