1

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?

3

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' );
}

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.