Using WordPress ‘register_nav_menu()’ PHP function

The register_nav_menu() WordPress PHP function registers a navigation menu location for a theme.

Usage

register_nav_menu( 'menu_location', 'Menu Description' );

Parameters

  • $location (string) – Menu location identifier, like a slug.
  • $description (string) – Menu location descriptive text.

More information

See WordPress Developer Resources: register_nav_menu

Examples

Register a Primary Menu

Function name: register_primary_menu

This code registers a primary menu location for your theme.

function register_primary_menu() {
  register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'register_primary_menu' );

Function name: register_footer_menu

This code registers a footer menu location for your theme.

function register_footer_menu() {
  register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'register_footer_menu' );

Register a Social Media Menu

Function name: register_social_menu

This code registers a social media menu location for your theme.

function register_social_menu() {
  register_nav_menu( 'social', __( 'Social Menu', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'register_social_menu' );

Register Multiple Menus

Function name: register_multiple_menus

This code registers multiple menu locations (primary, footer, and social) for your theme.

function register_multiple_menus() {
  register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
  register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
  register_nav_menu( 'social', __( 'Social Menu', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'register_multiple_menus' );

Register a Sidebar Menu

Function name: register_sidebar_menu

This code registers a sidebar menu location for your theme.

function register_sidebar_menu() {
  register_nav_menu( 'sidebar', __( 'Sidebar Menu', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'register_sidebar_menu' );