How To Programmatically Create a WordPress Menu

How can I create a new WordPress menu programmatically and assign the menu item to the new menu

You can use WordPress native function wp_create_nav_menu to create a new menu and using wp_update_nav_menu_item to assign the menu items

$menuname = "New Menu";
$menu_exists = wp_get_nav_menu_object( $menuname );

if( !$menu_exists){
    $menu_id = wp_create_nav_menu($menuname);

    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'));

}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments