Display conditionally php content depending on user role

I have a line that prints the contact information of a user in his profile. Only logged in user can see the contact information.

<?php 
  // Contact Info
?>

I have 2 user roles – supervisor, manager

Read More

What I am trying to achieve is that if the logged in user is a supervisor then he can see his own contact info, but he cannot see manager's contact.

But If the logged in user is a manager then he can see his own contact info, but he can also see supervisor's contact info.

I have been trying to use the standard if current_user_is function, but this will only display one or the other.

<?php global $current_user;
      get_currentuserinfo();
      if(current_user_is("manager"))
          echo 'Contact Info Here';
      else if(current_user_is("supervisor"))
          echo 'Contact Info Here';
      else if(current_user_is_not("manager"))
          echo 'Restricted contact, must be a manager';
?>

I just can’t figure out how to make this work, so it displays conditionally.

I also have these rules in the global rule, not sure how relevant it is :

$store_user = get_userdata( get_query_var( 'author' ) );
$store_info = get_store_info( $store_user->ID );
$user_meta = get_user_meta($store_user->ID);

Related posts

Leave a Reply

2 comments

  1. From the question, it looks like you’ll need to call the get_users function and pass the “supervisor” role to get users of that type and display their contact info to the “manager” role.

    As you’ve written it, the contact info being displayed is for the current user. You’ll need to grab another role’s contact info if that’s what you want to display.

    Untested but this may be close…

    //if current user is the manager...
    $supervisors = get_users( 'role=supervisor' );
    foreach ($supervisors as $supervisor) {
        echo '<span>' . esc_html( $supervisor->user_email ) . '</span>';
    }
    
  2. I’m nothing more than a beginner with php but using ‘OR’ may help you aggregate roles’ conditions on a single piece of content:

    <?php global $current_user;
        get_currentuserinfo();
        if(current_user_is("manager") OR current_user_is("supervisor"))
            echo 'Contact Info Here';
        else()
            echo 'Restricted contact, must be a manager';
    ?>