Meta data images (open graph, twitter etc) Fallback $site images

Hi all. Could someone maybe help me out with the best way to approach this? Im looking to load in (for example here) an open graph image. But I want to be able to fall back to a $site default image if the user fails to upload one for a specific page.

Currently I have the following pulling open graph image from each page. How would I add in the option to default to the $site ‘opengraphimage’ template if there isn’t one uploaded for the $page?

<?php foreach($page->images()->template('opengraphimage') as $image): ?>
<meta property="og:image" content="<?= $image->url() ?>">
<meta property="og:image:alt" content="<?= $image->alt() ?>">
<?php endforeach ?>

Thanks in advance for any help or suggestions.

You talk about a single image, why is that in a loop?

<?php if($page->images()->template('opengraphimage')->count(): ?>
  <?php foreach($page->images()->template('opengraphimage') as $image): ?>
    <meta property="og:image" content="<?= $image->url() ?>">
    <meta property="og:image:alt" content="<?= $image->alt() ?>">
  <?php endforeach ?>
<php else: ?>
  <?php if($image = $site->images()->template('opengraphimage')->first()): ?>
    <meta property="og:image" content="<?= $image->url() ?>">
    <meta property="og:image:alt" content="<?= $image->alt() ?>">
  <?php endif ?>
<?php endif ?>

But if it’s only one image anyway:

<?php
$image = $page->images()->template('opengraphimage')->first()?? $site->images()->template('opengraphimage')->first();
if($image):
?>
  <meta property="og:image" content="<?= $image->url() ?>">
  <meta property="og:image:alt" content="<?= $image->alt() ?>">
<?php endif ?>
1 Like

It’s the only way I could find to load in the image from a file upload in the panel. By loading in via the template having a specific name.

Is there a better way to do it? The only way I would find through the Kirby docs was to load in either the first image of the page or by file name. Which won’t work for what I need.

Thanks

That’s ok, you can leave it like that, but then just get the first image which results in a lot less code.

The alternative would be to use findBy()

<?php
$image = $page->images()->findBy('template', 'opengraphimage')?? $site->images()->findBy('template', 'opengraphimage');
if($image):
?>

Thanks for your help. Trying to get my head around Kirby.

Are there any benefits to using something like this plugin (https://github.com/pedroborges/kirby-meta-tags) over having open graph, twitter cards etc loaded in via snippets?

Thanks again.

It probably makes your life easier. But I don’t actually know the development status of the plugin for Kirby 3. Ping @pedroborges

Using a plugin always means depending on third parties rather than your own code, which can be a good thing if the code is likely better than your own could ever be. But of course, using a plugin also has the usual downsides (you don’t know in advance if the plugin will see continuous development, when bugs are fixed, etc.).

If I did my own, I’d probably wrap it in a plugin as well rather then single snippets - for future re-use.

Great thanks for your input. php has never been my strong point. Heres hoping Kirby helps me sort that out.

Have a good day :+1:

You wouldn’t be the first who developed their PHP coding skills with Kirby. It’s fun. And we are here to help.

Done.

Thanks for getting back so quickly, @pedroborges :slightly_smiling_face:

There was already a branch for v3 but with a few bugs. Thankfully @LeBen contributed with the needed fixes. I only had to review them, update the docs and package it according to the new pluginkit. I love this community!

2 Likes