0

I currently have a dive set up which I would like to display different bits of information when the user is logged in and has a specific role.

I have three roles 'administrator' 'adoption-agency' and 'shop-manager' which I would like to display a link to the admin area. Then I have a role named 'customer' which I would like to display a link to an internal page if customer is logged in and the user role matches. I have created an if else statement code but I seem to be getting errors.

I was wondering if someone could tell me what I am doing wrong.

    <?php

    if ( is_user_logged_in() && (current_user_can('administrator') || current_user_can('shop-manager') || current_user_can('adoption-agency'))) {
    echo '<p><span>Welcome: </span>' . $current_user->user_login . '</p>';
echo '<p><i class="fa fa-smile-o" ></i>
<a href="'. get_admin_url() .'">Post</a></p>';
     echo '<p><i class="fa fa-unlock-alt" ></i>
<a href="'. wp_logout_url() .'">Logout</a></p>';


    }else if( is_user_logged_in() && (current_user_can('customer')){





      echo   '<a href="' .get_page_link( get_page_by_title( account )->ID ).'">My account</a>';

}else{
    //default menu
echo wp_login_form(); }



    ?>
1

Your problem is that current_user_can() takes a capability not a user role. So, to check for an administrator, for example, you might use:

if ( current_user_can( 'manage_options' ) ) { ... }

because ordinarily only admins can manage options.

You'd have to tie your custom user roles to capabilities that correspond to their roles, which are defined for each role when you create it with add_role(). For a complete listing of built-in WP roles and their corresponding capabilities, see the codex.

| improve this answer | |
0

Jevuska is answer is corrected below detail of code check user role current user and display content conditionally.

if ( is_user_logged_in() ){
     $current_user = wp_get_current_user();
     $roles = $current_user->roles;  
     $level = 0;
     foreach( $roles as $role ){
       if( in_array( array( 'administrator', 'shop-manager', 'adoption-agency'), $role ) ){
         $level = 1;
         break;  
       }else if( in_array( array( 'customer'), $role ) ){
         $level = 2;
         break;    
       }
     }
     if( $level == 1 ){
           echo '<p><span>Welcome: </span>' . $current_user->user_login . '</p>';
           echo '<p><i class="fa fa-smile-o" ></i>
           <a href="'. get_admin_url() .'">Post</a></p>';
           echo '<p><i class="fa fa-unlock-alt" ></i>
           <a href="'. wp_logout_url() .'">Logout</a></p>';
     }else if( $level == 2 ){
         echo   '<a href="' .get_page_link( get_page_by_title( account )->ID ).'">My account</a>';

     }//else {
       // put default leave comment  
     //}
    }else{
      echo wp_login_form();     
}

OR

if ( is_user_logged_in() ){
     $current_user = wp_get_current_user();
     $roles = $current_user->roles;  
     if( in_array( 'administrator', $role ) || in_array( 'shop-manager', $role ) ||  in_array( 'adoption-agency', $role )){
           echo '<p><span>Welcome: </span>' . $current_user->user_login . '</p>';
           echo '<p><i class="fa fa-smile-o" ></i>
           <a href="'. get_admin_url() .'">Post</a></p>';
           echo '<p><i class="fa fa-unlock-alt" ></i>
           <a href="'. wp_logout_url() .'">Logout</a></p>';
     }else if( in_array( 'customer', $role ) ){
         echo   '<a href="' .get_page_link( get_page_by_title( account )->ID ).'">My account</a>';

     }//else {
       // put default leave comment  
     //}
    }else{
      echo wp_login_form();     
}
| improve this answer | |
  • i am receiving this warning : in_array() expects parameter 2 to be array, null given in on line 69, which refers to this part of the code if( in_array( 'administrator', $role ) || in_array( 'shop-manager', $role ) || in_array( 'adoption-agency', $role )){ – steamfunk Apr 23 '16 at 21:39
  • $current_user = wp_get_current_user(); print_r($current_user); check key roles assigned in users. codex.wordpress.org/Function_Reference/wp_get_current_user – user5200704 Apr 24 '16 at 4:13

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.