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
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(),
]
];
});