Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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 Feb 12 '12 at 23:55
add comment

4 Answers 4

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!';
    }
    ?>
share|improve this answer
    
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 Dec 13 '11 at 15:14
    
Updated my answer –  Giri Dec 13 '11 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 Dec 13 '11 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> –  Giri Dec 13 '11 at 15:29
    
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 Dec 13 '11 at 16:02
add comment

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;
    ?>
share|improve this answer
    
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 Dec 13 '11 at 15:27
    
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 Dec 13 '11 at 15:30
add comment
up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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