Run function at kirby start

A basic question I think - what is the best practice for running a plugin function as kirby initializes?

If you just want to run some code as Kirby initialises the best option would be to add a site.php file on the root (same level as index.php. Kirby will include that file automatically, you only have to add this line before your code:

<?php

$kirby = kirby();

// your code

See more: https://getkirby.com/docs/advanced/customized-folder-setup#site-php

Another option is to create a plugin at site/plugins. A plugin is just PHP code and you can do whatever you want there. It can either be a single file, e.g. site/plugins/myplugin.php or a folder. When using a folder, Kirby will include the php file which has the same name as the folder, e.g. site/plugins/myplugin/myplugin.php. From a plugin you can interact with Kirby using functions like: kirby(), site(), pages(), and many more. Take a look at some other helper functions available to you: https://getkirby.com/docs/cheatsheet#helpers

1 Like