I just tagged version 2.0.0 which is compatible with Kirby 3.
For Kirby 2 support, checkout the kirby-v2 branch.
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.
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?
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!
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.
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.
@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.