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.

Is there a way of obtaining the user ID of the profile being edited in wp-admin?

I know it's in the URL if you are editing a user, EX: ./wp-admin/user-edit.php?user_id=427. Could always $_GET['user_id'] to retrieve the user's ID, but what about when you're editing your own profile in wp-admin? The user ID wouldn't be in the URL. EX ./wp-admin/profile.php

Is there an easy or broad way of retrieving the user ID of the current user profile being edited in wp-admin?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

There is a global variable called … $user_id available on that page. Always.

From user-edit.php:

$user_id = (int) $user_id;
$current_user = wp_get_current_user();
if ( ! defined( 'IS_PROFILE_PAGE' ) )
    define( 'IS_PROFILE_PAGE', ( $user_id == $current_user->ID ) );

if ( ! $user_id && IS_PROFILE_PAGE )
    $user_id = $current_user->ID;
elseif ( ! $user_id && ! IS_PROFILE_PAGE )
    wp_die(__( 'Invalid user ID.' ) );
elseif ( ! get_userdata( $user_id ) )
    wp_die( __('Invalid user ID.') );
share|improve this answer

A little simplified. I didn't have access to a $user_id variable.

// If is current user's profile (profile.php)
if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
// If is another user's profile page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
    $user_id = $_GET['user_id'];
// Otherwise something is wrong.
} else {
    die( 'No user id defined.' );
}
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.