Custom field methods in Kirby 2?

Hi there,

I need to “sanitize” some text from text input fields in Kirby 2, that means I would need to create some custom field methods using some preg_replace that are then available to me globally.
While there is documentation on how to create your own field methods in Kirby 3, I couldn’t find anything for Kirby 2? Is there a way to do this?

Thank you!


Works perfectly, thanks!

Here’s the code I ended up with for future reference:


field::$methods['sanitize'] = function($field) {
  return preg_replace('/(\w)\/(\w)/', '$1&thinsp;/&thinsp;$2', $field->value());;

Actually, one more related question: How do I extend the str object with the same method? In my naive hope I just tried:

str::$methods['sanitize'] = functions($str) {
  return preg_replace('/(\w)\/(\w)/', '$1&thinsp;/&thinsp;$2', $str);

but that gives me an error Access to undeclared static property: Str::$methods. Is there any way to add a custom str method as well?

No, I don’t think so. You can create a custom class that extends the str class, but for only one function that probably wouldn’t make sense and a function would be more useful.

Where do I best store the functions I want to make globally accessible in Kirby 2?

In a plugin folder/file, e.g.


The name can be anything, helpers, functions, whatever

Ok, so any plugin code is just initialized globally, got it. Thanks!