0

In my menu i've got some menu items that i don't want to show to people who aren't logged in. One of these i only want to show to logged in users of a certain roles the other to everyone logged in.

So one of them is called 'My Printer'. This should be visible to everyone that is logged in.

The other is called 'Partners'. This should only be visible to users with the partner role.

I've tried using Role Scoper but i find it very difficult to use and it doesn't make a lot of sense. It also interferes with a lot of other things as well which i don't need.

Is there anyway i can hide menu items based on their role?

Thanks

EDIT

I've created the menu using the admin and placed that menu in a theme location. The menu items i want to hide are in this menu.

Thanks for the 2 answers below, but neither achieve what i'm after.

1
  • 1
    Please move your "edit" to an answer to explain how you solved the problem and mark it as "the answer" so it will help future visitors to the site.
    – EAMann
    Commented Feb 12, 2012 at 23:55

5 Answers 5

1

I've created the menu using the admin and placed that menu in a theme location. The menu items i want to hide are in this menu.

Thanks for the 2 answers below, but neither achieve what i'm after.

1

Check this out!

http://www.tomauger.com/2011/web-development/wordpress/wordpress-hiding-menu-items-from-users-based-on-their-roles-using-a-custom-walker

You have to override the Walker_Nav_Menu class!

1
  • Working code would be preferred. The link is dead.
    – nu everest
    Commented Jan 11, 2017 at 20:54
1
add_filter( 'wp_get_nav_menu_items', 'hide_menu_items_from_non_partners' );

function hide_menu_items_from_non_partners($items) {
    if ( current_user_can( 'partner' ) ) {
        return $items;
    }
    $ids_of_pages_to_hide = array(2,3,4); // you need to put here ids of pages you wish to hide
    foreach ( $items as $key => $item ) {
        if ( in_array($item->object_id, $ids_of_pages_to_hide ) ) unset( $items[$key] );
    }
    return $items;
}
0

You need to define something like this in your header.php file.

<?php
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
?>

Create a new role with the following code and put it in your functions.php

add_role('partners', 'Partners', array(
    'read' => true, // True allows that capability, False specifically removes it.
    'edit_posts' => true,
    'delete_posts' => true,
    'edit_published_posts' => true,
    'publish_posts' => true,
    'edit_files' => true,
    'import' => true,
    'upload_files' => true //last in array needs no comma!
));

Now lets say your partners has the capability "edit_published_posts". You can define the menu like this.

<?php
    if ( is_user_logged_in() ) {
            if(current_user_can('edit_published_posts')) {
            echo 'Welcome, Partner!';
            }
            else {
            echo 'Welcome, registered user!';
            }
    } 
    else {
        echo 'Welcome, visitor!';
    }
    ?>
5
  • Um, i don't understand how that's going hide menu items. I already have the roles so i don't need to add them.
    – Corbula
    Commented Dec 13, 2011 at 15:14
  • Updated my answer Commented Dec 13, 2011 at 15:15
  • I think you've misunderstood my original question. I'm trying to hide menu items based on a user's role, not display a message.
    – Corbula
    Commented Dec 13, 2011 at 15:21
  • That message is just a dummy text. You should replace those dummy text with your href links like this. <a href="http://www.yourwebsite.com/partnet-link-page">Partners</a> Commented Dec 13, 2011 at 15:29
  • 1
    I've updated the question. This is only good if i want to display something separate from the menu. Not if i want to hide an item of the menu.
    – Corbula
    Commented Dec 13, 2011 at 16:02
0

You could do this by creating different menus in Appearance -> Menus, and then use something like this to show the menu for the current role:

     <?php
        global $wp_roles;

        foreach ( $wp_roles->role_names as $role => $name ) :
            if (current_user_can( $role ) && $role == 'partner') {
                wp_nav_menu( array( 'theme_location' => 'partner-menu' ) );
            }
        endforeach;
    ?>
2
  • 1
    Thanks but that would only work if they were separate menus. I want to do this in one menu. So it's one menu with some of the items of that menu hidden, not a separate menu.
    – Corbula
    Commented Dec 13, 2011 at 15:27
  • 1
    mhm. in that case I would probably skip wp_nav_menu, and manually build the menu (which of course has an important downside of not being able anymore to change the menu in wp-admin). There may be a better solution, but that's far out of my league...
    – ptriek
    Commented Dec 13, 2011 at 15:30

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.