Jump to main content Jump to doc navigation

modX::getUser

Get the current authenticated User and assigns it to the modX instance.

Syntax

API Doc: modX::getUser()

modUser getUser ([string $contextKey = ''], [bool $forceLoadSettings = false])
  • $contextKey (string) Indicates the context to initialize, an optional context to get the user from.
  • $forceLoadSettings (bool) If set to true, will load settings regardless of whether the user has an authenticated context or not

Example

Get the current auth'ed user and print out its username.

$user = $modx->getUser();
echo $user->get('username');

Get the user's email address (stored in their profile) from 'web' context:

$user = $modx->getUser('web', true);
if (!$user) return '';
$profile = $user->getOne('Profile');
if (!$profile) return '';
print $profile->get('email');

Get an extended field from the user.

$user = $modx->getUser();
if (!$user) return '';
$profile = $user->getOne('Profile');
if (!$profile) return '';
$extended = $profile->get('extended');
print (isset($extended['custom_user_field'])) ? $extended['custom_user_field'] : '';

See Also