Override kirbytags without plugin

I would like to override the behavior of the default (image: *) kirbytag. Since I have my site/plugins directory stored in .gitignore, is it possible to do this for a specific page without having to register a custom plugin like described in the docs (https://getkirby.com/docs/reference/plugins/extensions/kirbytags#overriding-default-kirbytags)?

(I could also just rewrite .gitignore to include site/plugins/my-plugin/index.html but figured another approach could be cleaner.)

If you want to override the tag itself, you would have to register a plugin.

What exactly would you like to change? An alternative could be to use a hook… but depends on your use case.

I’m using an image lightbox plugin that requires some custom html tags that I would like to output in kirbytext. Tbh after searching the forum again I think I want to do exactly the same as this OP: Fancybox 3 in Kirby Text Field Hook?
So it looks like I have the choice between a hacky .gitignore file or using regex in a hook:balance_scale:

How do you include your other plugins? Submodules? Composer?

Composer. I’m using the “autoresize” and “pagetable” plugins

But with only these two “real” plugins, you might as well ignore them separately rather then the complete plugins folder? After all, it happens rather seldom that you don’t have any custom code in the plugins folder… (at least from my experience).

100% true. I initially thought of that but abandoned the thought because I might forget to add a future plugin to .gitignore after installing. But given the other options, this is definitely the easiest way :~)

…can I actually get content from the linked file in my new custom tags plugin? So that the plugin/index.php could look something like

<?php

Kirby::plugin('your/plugin', [
    'tags' => [
        'image' => [
            'attr' => [
                // list of attributes
            ],
            'html' => function($tag) {
                '<img data-caption="' . $file->caption() . '">
            }
        ]
    ]
]);

^ i know this is not correct at all :smile:

edit: ok I see now that this has been talked about in the forum post I mentioned earlier. I’ll just read the docs again and will probably figure it out on my own

You can reuse parts as explained in the cookbook recipe, but in your case, it probably makes more sense to do a proper override… in any case, you can’t just call $file without defining that variable first!

with a little help from the source code:

<?php

Kirby::plugin('your/plugin', [
    'tags' => [
        'image' => [
            'attr' => [
                'alt',
                'caption',
                'class',
                'height',
                'imgclass',
                'link',
                'linkclass',
                'rel',
                'target',
                'title',
                'width'
            ],
            'html' => function($tag) {
                if ($tag->file = $tag->file($tag->value)) {

                    $tag->src     = $tag->file->url();
                    $tag->alt     = $tag->alt     ?? $tag->file->alt()->or(' ')->value();
                    $tag->title   = $tag->title   ?? $tag->file->title()->value();
                    $tag->caption = $tag->caption ?? $tag->file->caption()->value();

                    $html = 
                    '<a 
                    href="' . $tag->src . '"
                    data-fancybox="files" 
                    data-width="'. $tag->file->width() . '"
                    data-height="'. $tag->file->height() . '"
                    data-caption="' . $tag->caption() . '"
                    >
                        <img src="' . $tag->file->resize(400)->url() . '" alt="' . $tag->alt . '">
                    </a>';

                    $html = str_replace(array("\r", "\n"), '', $html);

                    return $html;
                } 
            }
        ]
    ]
]);