Skip to content

Kirby 4.1.2

User methods

User methods are registered with the userMethods extension.

Default user methods

For a full list of default user methods, please check out the Reference.

Be aware that you cannot override these default user methods with any custom user method. If you want to override default methods, you can use a user model instead.

Getting started

You can extend the set of defined user methods in a plugin file.

/site/plugins/user-methods/index.php
Kirby::plugin('my/plugin', [
    'userMethods' => [
        'fullname' => function () {
            // provided there are firstname and lastname fields in the user blueprints
            return $this->firstname() . ' ' . $this->lastname();
        }
    ]
]);

This example shows the basic architecture of a user method. You define the method name with the key for the userMethods extension array. $this in the callback function refers to the $user object.

Working with method arguments

In some cases it might be helpful to be able to pass arguments to the method:

You can define arguments for a method like this:

/site/plugins/user-methods/index.php
Kirby::plugin('my/plugin', [
    'userMethods' => [
        'greetUser' => function ($text = 'Hello') {
            return $text . ', ' . $this->username();
        }
    ]
]);

And then use it like this:

<?= $user->greetUser('Dear') ?>