Setting Up Social Media Share Previews
To set up social media share previews, you can use the Open Graph protocol.
The Open Graph protocol defines how to set metadata to provide appropriate preview images, titles, descriptions, and other information when a web page is shared on social media platforms.
When a web page is shared on social media platforms such as Facebook or X (formerly Twitter), you can see a preview image, title, and brief description displayed along with the link.
This allows social media platforms to correctly recognize and use the metadata defined by the Open Graph protocol, which is inserted within the HTML <head> tag.
Metadata is defined using the <meta> tag, with the property attribute using values prefixed with og: and the content attribute defining the corresponding information.
<head>
<title>YouTube Page Metadata Shared on Facebook</title>
...
<meta property="og:url" content="https://www.youtube.com">
<meta property="og:title" content="YouTube">
<meta property="og:type" content="website">
<meta property="og:description" content="Enjoy the videos and music you love...">
<meta property="og:image" content="https://www.youtube.com/img/desktop/yt_1200.png">
...
</head>
Open Graph Protocol
According to the official Open Graph protocol documentation, the protocol was originally created by Facebook and was inspired by Dublin Core, link rel="canonical", Microformats, and RDFa.
It was originally designed as a way to define metadata that provides appropriate preview images, titles, descriptions, and other information when content is shared on Facebook, but it is now supported by many social media platforms.
The Open Graph protocol defines how to set metadata to provide rich preview information when a web page is shared on social media platforms.
It is very simple and straightforward. You just need to add metadata to your HTML.
Basic Metadata
Let's take a look at the basic metadata.
As you can easily see from the screenshot of the YouTube page preview shared on Facebook, the following metadata should be configured by default.
og:url- The URL of the page opened when clicking the previewog:title- The title of the web page displayed when sharedog:type- The type of the web page being shared (see detailed explanation)og:description-The description of the web page displayed when sharedog:image- The URL of the thumbnail image displayed when shared
<!-- The URL of the page opened when clicking the preview -->
<meta property="og:url" content="https://www.example.com">
<!-- The title of the web page displayed when shared -->
<meta property="og:title" content="Web Page Title">
<!-- The type of the web page being shared -->
<meta property="og:type" content="website">
<!-- The description of the web page displayed when shared -->
<meta property="og:description" content="Web Page Description">
<!-- The URL of the thumbnail image displayed when shared -->
<!-- Recommended image size: 1200x630px -->
<meta property="og:image" content="https://www.example.com/image.jpg">
Notes for Open Graph URLs
URLs used in Open Graph tags must be absolute URLs using the http:// or https:// protocol.
Basic Structure
<!DOCTYPE html>
<html lang="en" prefix="og: https://ogp.me/ns#">
<head>
<meta charset="utf-8">
<title>Basic Structure of Open Graph Protocol</title>
<meta property="og:url" content="https://www.example.com">
<meta property="og:title" content="Web Page Title">
<meta property="og:type" content="website">
<meta property="og:description" content="Web Page Description">
<meta property="og:image" content="https://www.example.com/image.jpg">
...
</head>
<body>
<h1>Basic Structure of Open Graph Protocol</h1>
<p>
This web page uses
the Open Graph protocol
to optimize social media sharing.
</p>
</body>
</html>
<html prefix="og: https://ogp.me/ns#">
<html lang="en" prefix="og: https://ogp.me/ns#">
The prefix="og: https://ogp.me/ns#" attribute is a namespace declaration used by the Open Graph Protocol. It declares that tags and attributes defined by the Open Graph Protocol will be used.
It is added to the <html> tag to specify that the Open Graph protocol is being used. This attribute informs social media platforms that the document includes Open Graph meta tags.
Although it is not strictly required, it is recommended because it can reduce errors when social media platforms parse the document and help them correctly recognize and process Open Graph meta tags.
There are community discussions indicating that some social media platforms may not display certain metadata if <html prefix="og: https://ogp.me/ns#"> is not included.
Optional Metadata
Optional metadata in the Open Graph protocol can provide additional information about a page or content beyond the basics. This helps create richer previews when shared on social media platforms. Below are key optional Open Graph meta tags.
og:locale
Indicates the language and regional settings of the content. The value format of the content attribute is language_TERRITORY.
<meta property="og:locale" content="en_US">
This metadata is used to help social media platforms accurately recognize the primary language and region of the content.
og:site_name
Indicates the name of the site.
<meta property="og:site_name" content="Site Name">
og:audio
Indicates the URL of an audio file associated with the web page.
<meta property="og:audio" content="https://www.example.com/audio.mp3">
Most social media platforms do not support the og:audio tag. Currently, support is limited and it is mainly available to Facebook partners. The og:audio tag alone does not implement an actual audio player on a web page. Additional work is required to add an audio player to a web page.
It is used when you want to display an associated audio file when sharing a web page on certain social media platforms, such as Facebook.
og:video
Indicates the URL of a video file associated with the web page.
<meta property="og:video" content="https://www.example.com/video.mp4">
Most social media platforms do not support the og:video tag. It indicates that the video is associated with the content shared on social media platforms such as Facebook and Twitter. The og:video tag alone does not implement an actual video player on a web page. Additional work is required to add a video player to a web page.
It is used when you want to display an associated video file when sharing a web page on some social media platforms, such as Facebook.
Detailed Explanation of og:type
A metadata tag used to specify the type of a web page.
<meta property="og:type" content="[type code]">
Role and Importance
- When a web page is shared on social media platforms, the platform understands the content of the web page based on
og:typeand displays it appropriately. - For example, if
og:typeis set toarticle, the platform recognizes the web page as a news article and displays the title, thumbnail image, summary, and other details appropriately. - Therefore, accurately setting
og:typehas a significant impact on how a web page is represented on social media.
Available Types
The following are representative types and examples:
article: News articles, blog posts, and similar content (additional properties:article:author,article:published_time,article:section)website: General websites (additional properties: None)video: Video content (additional properties:og:video,og:video:width,og:video:height, and others)book: Book information pages (additional properties:book:author,book:isbn, and others)profile: Personal profile pages (additional properties:profile:first_name,profile:last_name,profile:gender, and others)
Examples by Type
<meta property="og:type" content="article">
<meta property="og:title" content="How Artificial Intelligence Technology...">
<meta property="og:description" content="Recent advances in AI technology...">
<meta property="article:author" content="John Doe">
<meta property="article:published_time" content="2024-05-19">
<meta property="article:section" content="IT/Science">
<meta property="og:type" content="website">
<meta property="og:type" content="video.other">
<meta property="og:title" content="[Video Title]">
<meta property="og:description" content="[Video Summary]">
<meta property="og:image" content="[Thumbnail Image URL]">
<meta property="og:url" content="[Video URL]">
<meta property="og:video" content="[Video File URL]">
<meta property="og:video:width" content="1280">
<meta property="og:video:height" content="720">
<meta property="og:type" content="book">
<meta property="og:title" content="[Author Name] - [Book Title]">
<meta property="og:description" content="Brief summary of the book...">
<meta property="og:image" content="[Book Cover Image URL]">
<meta property="og:url" content="[Book Information Page URL]">
<meta property="book:author" content="[Author Name]">
<meta property="book:isbn" content="9788954614367">
<meta property="og:type" content="profile">
<meta property="og:title" content="John Doe">
<meta property="og:description" content="A web developer interested in artificial...">
<meta property="og:image" content="[Profile Image URL]">
<meta property="og:url" content="[Profile Page URL]">
<meta property="profile:first_name" content="John">
<meta property="profile:last_name" content="Doe">
<meta property="profile:gender" content="male">