wp_create_nav_menu( string $menu_name )

Creates a navigation menu.


Description Description

Note that $menu_name is expected to be pre-slashed.


Parameters Parameters

$menu_name

(string) (Required) Menu name.


Top ↑

Return Return

(int|WP_Error) Menu ID on success, WP_Error object on failure.


Top ↑

Source Source

File: wp-includes/nav-menu.php

function wp_create_nav_menu( $menu_name ) {
	// expected_slashed ($menu_name)
	return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Basic Example

    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', 'textdomain' ),
            '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', 'textdomain' ),
            'menu-item-url'    => home_url( '/custom/' ), 
            'menu-item-status' => 'publish'
    	) );
    }
    

You must log in before being able to contribute a note or feedback.