1

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

1 Answer 1

2

You have to register the menu location first.

Also, I've added else statement to be sure $menu_id is defined when menu exists. Otherwise, you'll see the PHP Notice about undefined variable. WP_DEBUG must be turned on when you develop, that's why you don't see the notice.

// Initialize name and location
$menu_name     = 'Simple Inventory Menu';
$menu_location = 'simple-inventory-menu';

// Register the location:
register_nav_menus(
    array(
        $menu_location => $menu_name,
    )
);

$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',
    ) );
} else {
    $nav_menu = get_term_by( 'slug', $menu_location, 'nav_menu' );
    $menu_id  = $nav_menu->term_id;
}

if ( ! has_nav_menu( $menu_location ) ) {
        $locations = get_theme_mod( 'nav_menu_locations' );
        $locations[ $menu_location ] = $menu_id; // no PHP notice here now
        set_theme_mod( 'nav_menu_locations', $locations );
}

You can expand the code to clean up modified 'nav_menu_locations' on the plugin deactivation. See register_deactivation_hook.

Complemented according the OP comment

If you already have registered menu locations (depends on the theme and navigation-menu-related plugins you use), and want your menu to appear there, just change the $menu_location value to the existing location (for example, 'footer-nav' or 'main-nav') and skip the register_nav_menus() step.

The downside is that this code will not work with any theme because of naming discrepancy. To make the code universal it have to be rewritten according to the mature requirements specification.

NO! Don't do the exposed above!

You'll have a lot of problems. Wait for me to get the right way. I also know where to utilize the menu you are talking about. That's why I'll develop it till them copmletely working solution.

3
  • Max, Thanks for the code. It works partially, by that I mean it created the menu but it still is not present on the main page. When I go into the GUI I can select the menu but it is not associated with the Header or Footer menu. Is there a way to make that happen. Ideally my menu (once fleshed out more) could replace the header menu. Sep 14, 2020 at 23:22
  • See the complemented answer.
    – Max Yudin
    Sep 15, 2020 at 9:49
  • Sorry, my suggestion does problems, not solutions. Wait a day. I also want to utilize the same functionality on some sites. Wait one day. I'll give out the working code.
    – Max Yudin
    Sep 15, 2020 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.