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.

So I understand that I can user current_user_can to check the role of the current logged in user. But what I would like to do is show these different fields to the site Admin.

So for example the contributor role may have different custom fields than the subscriber. Each role would see their relative fields but the Admin would see a whole list of all fields in a contributor profile page and subscriber profile page.

Is there a way to allow the admin to only see certain fields of a user profile page?

I hope this makes sense to you out there!

Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I'll post whole code which have to be in functions.php it's legit WP valid code, how it should be done :) This should work, of course you have to put your role name in switch.

UPDATED FOR PERFORMANCE:

add_action( 'show_user_profile', 'user_fields_for_admin', 10);

add_action( 'edit_user_profile', 'user_fields_for_admin', 10);

function user_fields_for_admin( $user ){

 switch ($user->roles[0]) {
  case 'SOME ROLE':
    echo '<b>This is the role specific fields</b>';
    echo 'fields....';
    break;
  case 'SOME ANOTHER ROLE':
    echo '<b>This is the role specific fields</b>';
    echo 'fields....';
    break;

 }

 } 
share|improve this answer
    
I have custom roles and it does not seem to work. Any suggestions? –  Fraggy May 18 '12 at 9:23
    
Sorry, yes this does work but you must put the roles in lowercase. Thanks! –  Fraggy May 18 '12 at 9:30
    
you're welcome :) And yes roles have to be the same as they are registered. You can lower the case of get_user_role return value, so you will always know that when you will use get_user_role you'll get lower case string, so you can always use lower case in comparing :) –  Tommixoft May 18 '12 at 9:38
    
PS> I edited the code to be more speedy and shorter, without using any other function to get user role, this will improve performance a bit :) –  Tommixoft May 18 '12 at 9:43
    
I have a problem with changing roles now. If I have fields 1, 2, 3 in admin role and 1, 2 in contrib, when I save I lose the data in field 3 if I then again change back to admin. –  Fraggy May 19 '12 at 16:43
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.