Meta Tags Plugin (Kirby 3)

I just tagged version 2.0.0 which is compatible with Kirby 3.

For Kirby 2 support, checkout the kirby-v2 branch.

Hi, Iā€™m testing this plugin to customize the way og:images are selected for my different page templates. Iā€™ve had some difficultly returning images, until I deleted the word ā€œPageā€ from the ā€˜namespace:imageā€™ function. I did so after a debug error highlighted it. But I fear I may regret this. Can you help me understand why itā€™s necessary?

Your blog post example:

'pedroborges.meta-tags.templates' => function ($page, $site) {
    return [
        'article' => [ // template name
            'og' => [  // tags group name
                'type' => 'article', // overrides the default
                'namespace:article' => [
                    'author' => $page->author(),
                    'published_time' => $page->date('Y-m-d'),
                    'modified_time' => $page->modified('Y-m-d'),
                    'tag' => ['tech', 'web']
                ],
                'namespace:image' => function(Page $page) {
                    $image = $page->cover()->toFile();
    
                    return [
                        'image' => $image->url(),
                        'height' => $image->height(),
                        'width' => $image->width(),
                        'type' => $image->mime()
                    ];
                }
            ]
        ]
    ];
}

And my working version (excerpt), which removes ā€˜Pageā€™:

                'namespace:image' => function($page) {
                    $image = $page->lead_image()->toFile();

                    return [
                        'image' => $image->url(),
                    ];

Thank you for your plugin!

You can remove it without issues.

1 Like

Thanks for confirming. Itā€™s been working well all week, but I just ran into trouble when I donā€™t have an image uploaded. It makes sense, I canā€™t preview a page until it has an image because the return is null. To get around that, can I add ā€œif($page->hasImages())ā€ to this function? I tried to experiment but without success.

        'image' => 	function($page) {
                    	$image = $page->photos()->first()->toFile(); 
                    	return [ $image->url() ];
                	}                    

One of the main reasons Iā€™m using this plugin is for Open Graph images. I originally thought to achieve this by making a few snippets for my header. I think I saw some advice to avoid snippets for such a scenario, but maybe it would be easier? Thank you!

Before you call a class method, you always have to make sure you have an instance of a class, an object, otherwise you get an error.

In your example above, if $page->photos()->first()->toFile() returns null instead of a file object, calling url() will result in an error.

Safe code:

'namespace:image' => function($page) {
    $image = $page->photos()->first()->toFile();
    return [
      'image' => $image ? $image->url() : '',
    ];

Where? What was the reasoning behind that advice?

1 Like

First, thank you for the quick reply ā€” that solved the issue. I get the logic, but is it too much to ask what the ā€˜?ā€™ and ā€˜:ā€™ syntax represent?

I guess it was just your own personal preference! :slight_smile:

I do see how the plugin makes the task more efficient, in the end. Itā€™s all relative I suppose ā€” I probably couldā€™ve written the snippets without relying on any support. But Iā€™m happy to learn this and am grateful for the help.

Its the so-called ternary operator in PHP, a conditional operator that can serve as a shortcut for a simple if statement.

The long form with an if statement would be

$url = '';
if ( $image ) {
  $url = $image->url();
}

Then use the variable inside the array:

return [
      'image' => $url
    ];

As you can see, using the ternary operator is much more comfortable and less code for these kind of things.

1 Like

Thank you. That makes sense and seems really useful. I know some of this is beyond Kirby itself, so I appreciate the reference articles.

Last clarification (not urgent!)ā€¦ I noticed that the empty quotes do not result in:

<meta property="og:image" content="">

Is that likely because of the plugin (or browser), seeing an empty result and therefore not generating the property?

Iā€™m afraid I donā€™t really understand what you mean? What is the result? I would expect no output if there is no image URL.

Sorry, Iā€™m just revealing my inexperience with PHP. I was trying to understand if the '' in 'image' => $image ? $image->url() : '', says ā€œthereā€™s no contentā€, or if it negates ā€˜imageā€™ altogether.

This is why I wondered if it was more of the pluginā€™s own interpretation ā€” seeing there was nothing for ā€˜imageā€™ and therefore not generating an ā€˜og:imageā€™ property. Iā€™ll learn more about empty values! Please disregard!

ps. I am on the verge of launching my first Kirby site, a year in the making. And I truly donā€™t think I couldā€™ve made it using anything else. I love Kirby. Thank you for all your support in this forum.

Hi @andremora, the plugin will omit the meta properties when the value returned from the configuration is null or an empty string.

1 Like

@pedroborges Is your plugin still under development? I noticed some open issues and PRs? Donā€™t know how critical these areā€¦

Not under development since itā€™s been stable for my use cases for many years. I consider it done feature-wise.

From what I can see only two PRs for edge cases are worth merging. Iā€™ll merge and test them this week.

1 Like