Progmatically adding menu links to the default (Top) or Footer menu

Question

I need to create a menu – add links to it and then make it active – all from within my plugin at activation time.
I have been able to create the menu and add links to it. But it does not become ‘active’ I can go in via the WP Interface and see the menu and then add it to the “footer” or make it the default “Top” menu but all of that requires user input. I want it to be done by plugin activation.
Code below:

    $menu_name   = 'Simple Inventory Menu';
$Simple_menulocation = 'simple-inventory-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'  =>  __( 'Admin', 'textdomain' ),
        'menu-item-url'    => home_url( '/index.php/admin/' ), 
        'menu-item-status' => 'publish'
    ) );
}


if( !has_nav_menu($Simple_menulocation) ){
        $locations = get_theme_mod('nav_menu_locations');
        $locations[$Simple_menulocation] = $menu_id;
        set_theme_mod( 'nav_menu_locations', $locations );
    }
0
Steve Devine 4 months 0 Answers 11 views 0

Leave an answer