skip to Main Content

i am new to WordPress and trying to find a way to add menus based on user roles. I have (guest user or non logged in user) vendor and subscriber roles. I want to display different primary menus depending on what role user is. Example code taken from https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/

Example code not working

function nav_menu_args( $args = '' ) {

if( is_user_logged_in('vendor')) { 

    ( 'primary-menu' == $args['theme_location'] ) { 
       $args['menu'] = 'VendorMenu';
  }

} 
else if
    ( is_user_logged_in('subscriber'))  {
    ( 'primary-menu' == $args['theme_location'] ) { 
    $args['menu'] = 'SubscriberMenu';
    }
}

 else 
     (!is_user_logged_in) { 
      ( 'primary-menu' == $args['theme_location'] ) { 
    $args['menu'] = 'PrimaryMenu';
}
    } 

return $args;
}
  add_filter( 'wp_nav_menu_args', 'nav_menu_args' );

At the moment it changes all menus to vendor menu including primary top and footer, all menus have the same menu.

Any help or guidance in the right direction will be much appreciated.

I am adding the code in to the custom/child theme functions.php file.

2

Answers


  1. Chosen as BEST ANSWER

    This worked for me (slightly different method but same outcome)

    if ( current_user_can( 'subscriber' ) ) { 
    
        //display menu
    
    }
    
     if ( current_user_can( 'vendor' ) ) { 
    
        //display menu
    
    }
    

  2. you can use current_user_can() function to check user capabiltiy or role. see this link.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search