Site methods
Site methods are registered with the siteMethods
extension.
Default site methods
For a full list of default site methods, please check out the Reference.
Getting started
You can extend the set of defined site methods very easily in a plugin file.
Kirby::plugin('my/plugin', [
'siteMethods' => [
'getSeoTitle' => function () {
return $this->seoTitle()->isNotEmpty()? $this->seoTitle() : $this->title();
}
]
]);
This example shows the basic architecture of a site method. You define the method name with the key for the siteMethods
array. $this
in the callback function is the $site
object.
Working with method arguments
In some cases it might be helpful to be able to pass arguments to the method:
<?php dump($site->contact('Bastian Allgeier')) ?>
The definition for such a method with arguments is very straightforward:
Kirby::plugin('my/plugin', [
'siteMethods' => [
'contact' => function ($name = '') {
$contact = $this->contacts()->toStructure()->findBy('name',$name);
return $contact;
}
]
]);