Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp create nav menu


Create a Navigation Menu. This function can be used to programmatically create a navigation menu which will be added to the Menus page under Appearance.


<?php $menu_id wp_create_nav_menu($menu_name?>


(string) (required) The title of the menu to create.
Default: None


To check if a menu exists first and then create it if it doesn't exists, and finally add menu items to it, use:

// Check if the menu exists
$menu_name = 'My First Menu';
$menu_exists = wp_get_nav_menu_object( $menu_name );

// If it doesn't exist, let's create it.
if( !$menu_exists){
    $menu_id = wp_create_nav_menu($menu_name);

	// Set up default menu items
    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __('Home'),
        'menu-item-classes' => 'home',
        'menu-item-url' => home_url( '/' ), 
        'menu-item-status' => 'publish'));

    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __('Custom Page'),
        'menu-item-url' => home_url( '/custom/' ), 
        'menu-item-status' => 'publish'));


Return Values

The Menu ID or WP Error Object in case of failure.

Source File

wp_create_nav_menu() is located in wp-includes/nav-menu.php.


wp_get_nav_menu_items, wp_get_nav_menu_object, wp_update_nav_menu_item

See also index of Function Reference and index of Template Tags.