0

Frustrated as to why even a WordPress sample code fails. Writing a simple plugin that will switch which menu I am displaying based on the user's logged-in state and role. I am saving the present menu, in the event

The code area in question is as follow:

add_filter( 'wp_nav_menu_args', 'MenuSelector' );

function MenuSelector( $args = '')
{ 
    $menu_name = 'primary';
    $locations = get_nav_menu_locations();  
    $menu_id = $locations[ $menu_name ] ;   
    $menu = wp_get_nav_menu_object($menu_id);
    $PrimaryMenu =  $menu['name'];

......
}

This is identical to the code suggested on the WordPress reference. https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object

Even so, the following error appears:

Notice: Undefined index: primary in /home/dskurth/public_html/wp-content/plugins/MenuSelector/main.php on line 105.

Line 105 is: $menu_id = $locations[ $menu_name ] ;

So basically it is saying that the definition of the menu name, ie: 'primary' is an undefined variable. This is even after following the WordPress definition of the array returned by:

$locations = get_nav_menu_locations();

Why?

0

So, the code in the documentation assumes that the menu you're trying to get is set as the primary menu. If the menu you're trying to get isn't the one set as primaryin Apperance -> Menus, this error will show. I suggest you try to work with their name or slug instead.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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