Kirby comes with a set of default KirbyTags for things like including images, links, dates or videos. See the full list of included KirbyTags .
/site/plugins/your-plugin/index.php
<?php
Kirby::plugin('your/plugin', [
'tags' => [
'wikipedia' => [
'html' => function($tag) {
return '<a href="http://wikipedia.org">Wikipedia</a>';
}
]
]
]);
/site/plugins/your-plugin/index.php
<?php
Kirby::plugin('your/plugin', [
'tags' => [
'wikipedia' => require_once __DIR__ . '/tags/wikipedia.php'
]
]);
And then in /tags/wikipedia.php
/site/plugins/your-plugin/tags/wikipedia.php
<?php
return [
'html' => function($tag) {
return '<a href="http://wikipedia.org">Wikipedia</a>';
}
];
You can override Kirby's default KirbyTags by creating a plugin with the same KirbyTag name as the original.
/site/plugins/your-plugin/index.php
<?php
Kirby::plugin('your/plugin', [
'tags' => [
'image' => [
'attr' => [
// list of attributes
],
'html' => function($tag) {
// your code here
}
]
]
]);
If you want to add attributes (e.g. (wikipedia: class: my-class)
), you can add them as an attr
array like this:
<?php
Kirby::plugin('your/plugin', [
'tags' => [
'wikipedia' => [
'attr' => [
'class'
],
'html' => function($tag) {
return '<a class="' . $tag->class . '" href="http://wikipedia.org">Wikipedia</a>';
}
]
]
]);
The properties of the $tag
object can be accessed like this:
$tag->value
// return the value of the tag
// the class attribute
$tag->class
// the width attribute
$tag->width
$tag->attrs
$tag->parent()
// all files of the page
$tag->parent()->files()
// a single file by name
$tag->parent()->file('content.jpg')
$tag->option('some.option')
$tag->type()
// returns the name of the KirbyTag
In addition to your own KirbyTag plugins, you can also hook into the KirbyTags parser. This is very useful if you want to parse the text before or after the KirbyTags parser kicks in with additional regular expressions for example.
/site/plugins/your-plugin/index.php
<?php
Kirby::plugin('your/plugin', [
'hooks' => [
'kirbytags:before' => function ($text, array $data = [], array $options = []) {
// KirbyTags have not been parsed
$text = preg_replace_callback('/some-regex/', function () {
// whatever you want to replace
}, $text);
return $text;
}
]
]);
/site/plugins/your-plugin/index.php
<?php
Kirby::plugin('your/plugin', [
'hooks' => [
'kirbytags:after' => function ($text, array $data = [], array $options = []) {
// KirbyTags have already been parsed
$text = preg_replace_callback('/some-regex/', function () {
// whatever you want to replace
}, $text);
return $text;
}
]
]);
You can also reuse parts of the original KirbyTag in your custom tags.