The register_nav_menus() WordPress PHP function registers navigation menu locations for a theme.
Usage
register_nav_menus( array( 'primary_menu' => __( 'Primary Menu', 'text_domain' ), 'footer_menu' => __( 'Footer Menu', 'text_domain' ), ) );
Parameters
$locations
string[] (Optional): Associative array of menu location identifiers (like a slug) and descriptive text. Default: array()
More information
See WordPress Developer Resources: register_nav_menus()
Examples
Register a Primary and Footer menu
Registers a primary and footer menu for a theme.
function mytheme_register_nav_menu() { register_nav_menus( array( 'primary_menu' => __( 'Primary Menu', 'text_domain' ), 'footer_menu' => __( 'Footer Menu', 'text_domain' ), ) ); } add_action( 'after_setup_theme', 'mytheme_register_nav_menu', 0 );
Register menus for a PluginBuddy Mobile and a custom Footer menu
Registers a PluginBuddy Mobile Navigation menu and a custom Footer menu.
register_nav_menus( array( 'pluginbuddy_mobile' => 'PluginBuddy Mobile Navigation Menu', 'footer_menu' => 'My Custom Footer Menu', ) );
Create menus from custom taxonomies
Creates menus for each term in the ‘country-category’ taxonomy.
$tax = 'country-category'; $terms = get_terms( $tax, [ 'hide_empty' => false, ] ); $args = array( 'primary' => __( 'Primary Menu', 'khoipro' ), 'secondary' => __( 'Secondary Menu', 'khoipro' ) ); foreach ( $terms as $term ) { $args[ $term->slug ] = $term->name; } register_nav_menus( $args );
Refactor menu slugs
Update menu location after renaming the slug in register_nav_menu().
function cebbi_update_menu_location() { $nav_menu_locations = get_theme_mod('nav_menu_locations'); if (isset($nav_menu_locations['old-slug'])) { if (!isset($nav_menu_locations['new-slug']) || $nav_menu_locations['new-slug'] === 0) { $nav_menu_locations['new-slug'] = $nav_menu_locations['old-slug']; unset($nav_menu_locations['old-slug']); set_theme_mod('nav_menu_locations', $nav_menu_locations); } } } add_action('after_setup_theme', 'cebbi_update_menu_location');
Register a sidebar menu
Registers a sidebar menu for a theme.
function mytheme_register_sidebar_menu() { register_nav_menus( array( 'sidebar_menu' => __( 'Sidebar Menu', 'text_domain' ), ) ); } add_action( 'after_setup_theme', 'mytheme_register_sidebar_menu', 0 );