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.

How do I display a custom field for a specific role, but not display it on the profile page of the admin? When admin goes to edit a profile user that this role, the field will visible.

I created two fields for Subscriber role, but the fields appears on admin profile page. I need to display these fields only if admin goes to the profile page of subscribers.

function show_custom_field_for_subscriber(){

    if( current_user_can('administrator') || current_user_can('subscriber') ) {

    //code html for display the fields

}

add_action( 'show_user_profile', 'show_custom_field_for_subscriber' );
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You can use the WP_User class and the has_cap($role) method. The show_user_profile action passes a WP_User object as a parameter to the called function.

http://codex.wordpress.org/Class_Reference/WP_User#has_cap.28.24cap.29

add_action('show_user_profile', 'my_add_extra_profile_fields');
function my_add_extra_profile_fields($user) {
    if ($user->has_cap('subscriber'))
    {
        //Code here
    }
}
share|improve this answer
    
Thanks George Yates, It worked perfect! :) –  enoquenroll Aug 7 '13 at 20:47
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.