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.

I'm trying to show the contents only for certain registered users. All users have the role of "agent". To hide the contents of an unregistered user, I use this code:

<?php if  ( current_user_can( 'agent' )  ){ ?>
    ///here the content
<?php } ?>

Now I want to show the contents only for users with id 8,9, with this code:

<?php if  ( current_user_can( 'agent=8,9' )  ){ ?>
    ///here the content
<?php } ?>`,`<?php if  ( current_user_can( 'author_name=Jacks,David' )  ){ ?>
    ///here the contents
<?php } ?>

But nothing happens!!! Please help.

share|improve this question

1 Answer 1

To show content only for users with role 'agent', try to use this snippet:

<?php if ( ( $user = wp_get_current_user() ) && in_array( 'agent', $user->roles ) ) { ?>
    ///here the content
<?php } ?>

If you want to block users by ID, check the following snippet:

<?php if ( ( $user = wp_get_current_user() ) && in_array( 'agent', $user->roles ) && in_array( $user->ID, array( 8, 9 ) ) ) { ?>
    ///here the content
<?php } ?>
share|improve this answer
    
Where to send the id or the names of agents? –  Shklyar Sergio Apr 6 '12 at 11:27
    
This code works for me, but I would like for some users:<?php if ( current_user_can( 'agent' ) ){ ?> ///here the content <?php } ?> –  Shklyar Sergio Apr 6 '12 at 11:29
    
Function current_user_can checks only user capabilities, not roles. As an option you can add agent capability into agent role, and then this function will work. –  Eugene Manuilov Apr 6 '12 at 11:47
    
Added for checking for users' IDs. –  Eugene Manuilov Apr 6 '12 at 14:12
    
Eugene Thank you very much, worked like clockwork. –  Shklyar Sergio Apr 6 '12 at 14:50

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.