Most elegant way to do meta data (facebook, twitter, google)

What is the most elegant data to do the meta tags?
for facebook I have this for example

  <meta property="og:title" content="<?php echo $page->title() ?> | <?php echo $site->title() ?>"</title>
  <meta property="og:description" content="<?php echo $page->text()->short(100) ?> <?= $site->description() ?>"/>
  <meta property="og:image" content="<?php echo $page->images()->first(); ?>"/>

The Meta Tags plugin may help you:

Yup, the Meta Tags plugin is the way to go (its awesome :slight_smile: ), combined with some shared global fields on every page. I went into detail in this post a few days ago.

1 Like

As a bonus, heres the meta tags plugin config iā€™m using on my website, complete with fallback imagery for sharing on twitter and facebook. Just pop fallback image in your assets/images folder.

$image = new Asset('assets/images/twitter-share.jpg');
$image = new Asset('assets/images/facebook-share.jpg');

It checks the current page for a share image. If the field is empty, it falls back to checking the site page for a share image. If the worst happens, and thats also empty, it falls back to a generic share image from the assets folder.

Makes use of the focusCrop plugin incase the image is larger the the share dimensions.

Enjoy :slight_smile:

c::set('meta-tags.default', function(Page $page, Site $site) {
    return [
        'title' => $page->isHomePage()
          ? $site->seotitle()
          : $page->seotitle().' | '.$site->seotitle(),
          'meta' => [
              'robots' => 'index,follow,noodp',
              'description' => $page->isHomePage()
                ? $site->seometa()
                : $page->seometa(),
              'keywords' => $page->isHomePage()
                ? $site->seokeywords()
                : $page->seokeywords(),
              'robots' => 'index,follow,noodp',
          ],
        'link' => [
            'canonical' => $page->url()
        ],
        'og' => [
            'title' => $page->isHomePage()
                ? $site->seotitle()
                : $page->seotitle().' | '.$site->seotitle(),
            'type' => 'website',
            'site_name' => $site->title(),
            'url' => $page->url(),

            'image' => function ($page) {
                $image = $page->shareimagefacebook()->toFile();
                if ($image) {
                    return $image->focusCrop(1200, 630)->url();
                } else {
                    $image = site()->shareimagefacebook()->toFile();
                    if ($image) {
                        return $image->focusCrop(1200, 630)->url();
                    } else {
                        $image = new Asset('assets/images/facebook-share.jpg');
                        return $image->url();
                    }
                }
            },

            'description' => $page->isHomePage()
                ? $site->seometa()
                : $page->seometa().' | '.$site->seotitle(),
        ],
        'twitter' => [
            'title' => $page->isHomePage()
                ? $site->seotitle()
                : $page->seotitle().' | '.$site->seotitle(),
            'card' => 'summary',
            'site' => $site->twitterusername(),
            'creator' => $site->twitterhandle(),
            'image' => function ($page) {
                $image = $page->shareimagetwitter()->toFile();
                if ($image) {
                    return $image->focusCrop(1024, 512)->url();
                } else {
                    $image = site()->shareimagetwitter()->toFile();
                    if ($image) {
                        return $image->focusCrop(1024, 512)->url();
                    } else {
                        $image = new Asset('assets/images/twitter-share.jpg');
                        return $image->url();
                    }
                }
            },
            'url' => $page->url(),
            'description' => $page->isHomePage()
                ? $site->seometa()
                : $page->seometa().' | '.$site->seotitle(),
        ]
    ];
}); 
1 Like