I'm new in wordpress. I want to hide/show some menus of my main menu according to the user. I mean I want to add some conditions for each menu but I don't know how to do it, if it's possible

  • Share what you've tried till now. Give us some codes, so we can understand this properly. – CodeMascot Dec 18 '16 at 16:05
  • I'm building a website, there is some user type like student, professor, ... and in wordpress, as I know, the menu is created in admin dashboard and I think it is static (correct me if I'm wrong), then I want to make it dynamic (add menu in code so that I can make some conditions), I've no code about this because I don't know where to do it. – Dina Dec 18 '16 at 16:26
  • If you don't know where to start I suggest using a search engine and finding a tutorial that you can understand. This one looks good, for example. – montrealist Dec 18 '16 at 16:37
  • User type means "User Role". I mean the student, professor are the user roles, right ? @Dina – CodeMascot Dec 18 '16 at 16:55
  • yes that's it, but I don't want to confuse default wordpress user roles (suscriber, ...) with these roles :) @the_dramatist – Dina Dec 18 '16 at 17:14

Actually I think you need to hide or show the menu items based on "User Role". So here a possible solution would be creating menus for each roles and showing them by condition based on "User Role". Here is the code-

function the_dramatist_menu_change( $args = '' ) {
    $current_user = wp_get_current_user();
    if( in_array('your-user-role', $current_user->roles) ) {
        $args['menu'] = 'Menu 1'; // Menu name as string
    } else {
        $args['menu'] = 'Menu 2'; // Menu name as string
    // Add more else if condition based on your user roles.
    return $args;

add_filter( 'wp_nav_menu_args', 'the_dramatist_menu_change' );

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.