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.

On the user-profile page of my site I want to allow admin role users to be able to edit specific user-meta fields of subscriber role users.

How can I distinguish between the two user IDs.

The current logged in user is the admin user

get_current_user(); // returns the admin role user id

but I need to be able to access the subscriber ID to set the meta-data

update_user_meta($subscriber_user_id,"name","value");

How can i populate the $subscriber_user_id value correctly?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

On the "profile page", i.e. user-edit.php in the admin back-end, the user ID of the profile currently being edited lives in the $user_id global.

Hence:

global $user_id;
update_user_meta( $user_id, 'key', 'value' );

is the essence of what you are looking for.

Whether the current user is an admin needs to be checked only if you have the edit_users capability assigned to roles other than admins, which by default is not the case.

If you want the admins to only be able to save the metadata for subscribers, while having users of other roles, then you will have to check the edited users role before saving, obviously.

So, for the sake of completeness, the same with both checks in place:

global $current_user, $user_id;

/* user object of user being edited */
$edited_user = new WP_User( $user_id );

/* verify both users roles */
if (
    in_array( 'administrator', $current_user->roles ) &&
    in_array( 'subscriber', $edited_user->roles )
) {
    update_user_meta( $edited_user->ID, 'key', 'value' );
}
share|improve this answer

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.