Search
Site Essentials for Statamic

Site Essentials for Statamic

Open Graph Metadata

The general metadata builder provides various methods to add common metadata information to your site, such as keywords, titles, and descriptions.

To access the general metadata builder using the Metadata facade, we can call the openGraph method:

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5Metadata::openGraph();

#Title

The title method queues a <meta property="og:title" content="..."> tag. By default it binds to the $title variable.

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5// Adjusting the returned variable.
6Metadata::resolve('title', function (array $context) {
7 return 'An even better title!';
8});
9 
10// Changing which variable is used.
11Metadata::openGraph()->title('$seo_title');

#Type

The type method queues a <meta property="og:type" content="..."> tag. By default it sets the type to website.

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5Metadata::openGraph()->type();
6Metadata::openGraph()->type('video.movie');

#URL

The url method queues a <meta property="og:url" content="..."> tag. By default it binds to the $current_full_url variable.

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5// Adjusting the returned variable.
6Metadata::resolve('current_full_url', function (array $context) {
7 return 'the-url-to-use';
8});
9 
10// Changing which variable is used.
11Metadata::openGraph()->url('$a_custom_variable_name');

#Description

The description method queues a <meta property="og:description" content="..."> tag. By default it binds to the $meta_description variable.

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5// Adjusting the returned variable.
6Metadata::resolve('meta_description', function (array $context) {
7 return 'The custom description.';
8});
9 
10// Changing which variable is used.
11Metadata::openGraph()->description('$description_field_name');

#Site Name

The siteName method queues a <meta name="site_name" property="og:site_name" content="..."> tag. By default it binds to the $meta_site_name variable.

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5// Adjusting the returned variable.
6Metadata::resolve('meta_site_name', function (array $context) {
7 return 'Stillat';
8});
9 
10// Changing which variable is used.
11Metadata::openGraph()->siteName('$the_site_name');

#Locale

The locale method queues a <meta property="og:locale" content="..."> tag. By default it will use the locale of the current site. This behavior can be modified by supplying a custom variable name or callback function.

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5Metadata::openGraph()->locale('$the_locale_name');
6Metadata::openGraph()->locale(function(array $context) {
7 return 'The locale';
8});

#Locale Alternate

The localeAlternate method queues multiple <meta property="og:locale:alternate" content=".."> tags. By default it will use the of the current entry. The logic for this is similar to the following example:

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4use Stillat\StatamicSiteEssentials\Metadata\DataHelpers;
5use Stillat\StatamicAttributeRenderer\RepeatableValue;
6use Stillat\StatamicSiteEssentials\Data\Entries\EntryLocales;
7 
8Metadata::openGraph()->localeAlternate(function (array $context) {
9 $entry = DataHelpers::getEntryFromContext($context);
10 
11 if (! $entry) {
12 return new RepeatableValue([]);
13 }
14 
15 /** @var EntryLocales $entryLocales */
16 $entryLocales = app(EntryLocales::class);
17 $locales = $entryLocales->getLocalesForEntry($entry);
18 
19 return new RepeatableValue($locales);
20});

#Image Asset

The imageAsset helper method accepts an Asset instance and generates the following meta tags based on the asset's information:

1<meta name="image" property="og:image" content="...">
2<meta property="og:image:width" content="...">
3<meta property="og:image:height" content="...">
4<meta property="og:image:type" content="...">
5<meta property="og:image:alt" content="...">

#Image

The image helper method queues a <meta name="image" property="og:image" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Image Type

The imageType method queues a <meta property="og:image:type" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Image Width

The imageWidth method queues a <meta property="og:image:width" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Image Height

The imageHeight method queues a <meta property="og:image:height" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Image Alt

The imageAlt method queues a <meta property="og:image:alt" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Video Asset

The videoAsset method method accepts an Asset instance and generates the following meta tags based on the asset's information:

1<meta name="video" property="og:video" content="...">
2<meta property="og:video:type" content="...">
3<meta property="og:video:width" content="...">
4<meta property="og:video:height" content="...">

#Video

The video helper method queues a <meta name="video" property="og:video" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Video Type

The videoType method queues a <meta property="og:video:type" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Video Width

The videoWidth method queues a <meta property="og:video:width" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

#Video Height

The videoHeight method queues a <meta property="og:video:height" content="..."> tag. By default it does not contain a value, and is generally used to construct a large amount of meta tags at once.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.