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

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • CrewMark
  • Chris Fickling
  • deJaya
  • eydolan
  • Following Sea
  • Lefthandmedia
  • Murray Wood
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • Nick Clark
  • A. Moreno
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$311 per month—let's make that $500!

Learn more