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 );
}
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 );
}
Share
Improve this question
edited Sep 14, 2020 at 15:56
Tom J Nowell♦
61k7 gold badges79 silver badges148 bronze badges
asked Sep 14, 2020 at 15:36
Steve DevineSteve Devine
111 bronze badge
1 Answer
Reset to default 2You 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.