Skip to content

Kirby 3.9.8

$user

The $user object represents a single Panel or frontend user.

How to create a $user object

Before you can call the methods of the User class, you need a $user object.

The $user object is available via the $kirby object:

The current user

$user = $kirby->user();

A specific user by user id or email address

$user = $kirby->user('you@yourdomain.com');

The first user of the users collection

$user = $kirby->users()->first();

Before calling any of the methods of the User class, verify that you have a valid User object.

Examples

With the $user object in place, you can use the methods of the user class to get information about the user or modify the user object.

Check if the user is an admin

if ($user->isAdmin()) {
  echo "Hey, great, you can do anything you like!"
}

Convert user object to array

$userData = $user->toArray();

Change the user name programmatically

$user->changeName('new-name');

Fetch all files belonging to the user object

$userFiles = $user->files();

To keep the examples short, we don't check if the (user) object exists in the examples above. In your code, you should always do that.