Skip to content

Kirby 4.3.0


Become any existing user or disable the current user

$kirby->impersonate(?string $who = null, ?Closure $callback = null): mixed|null


Name Type Default Description
$who string|null null User ID or email address,
null to use the actual user again,
'kirby' for a virtual admin user or
'nobody' to disable the actual user
$callback Closure|null null Optional action function that will be run with
the permissions of the impersonated user; the
impersonation will be reset afterwards

Return type



Type Description

Parent class



There are four options for the $who parameter:

  • a username (impersonates the specific user with their permissions)
  • 'kirby' (impersonates the allmighty user with full permissions)
  • 'nobody' (disables the currently logged in user)
  • null (resets the impersonation)



$kirby = kirby();

  'author' => 'Homer Simpson'

The impersonation will be active for all code that runs in the current request after the call to $kirby->impersonate(). If you want to limit the impact to a single operation, call the method with a callback (see below).

With callback

If you only want a single operation to run with different privileges, use a callback:


$kirby = kirby();
$result = $kirby->impersonate('kirby', function () {
    'author' => 'Homer Simpson'

  return 'this will be returned to $result above';

The impersonation automatically gets reset to the previous value after the callback returns.