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