Using Open Graph Protocol on your Website

Zafir Sk Heerah
4 min readFeb 14, 2024

--

Example of the Components for the OGP Metadata

What is the Open Graph Protocol?

The Open Graph Protocol (OGP) is a set of meta tags that you can add to your website’s HTML to control how your content appears when shared on social media platforms. Developed by Facebook, OGP allows you to define structured information about your web pages, such as titles, descriptions, images, and more. These tags provide a standardized way for social media platforms to understand and display your content consistently.

OGP Content on LinkedIn

What is the use of this protocol?

The primary purpose of the Open Graph Protocol is to enhance the presentation of your website’s content on social media platforms. By including OGP meta tags in your HTML, you gain control over how your content appears in shared links. This means you can specify the title, description, image, and other relevant information, ensuring that your content is displayed in a visually appealing and informative way when shared on platforms like Facebook, Twitter, LinkedIn, and others.

How to use the protocol in an HTML page?

Basic Setup

To implement Open Graph Protocol on your website, you need to include specific meta tags in the <head> section of your HTML document. Here's a basic example.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Open Graph Protocol meta tags -->
<!-- important ones -->
<meta property="og:title" content="Your Page Title">
<meta property="og:description" content="Brief description of your content">
<meta property="og:image" content="URL to your featured image">
<meta property="og:url" content="URL of the page">
<!-- optional ones -->
<meta property="og:type" content="website">

<!-- Add more tags as needed -->

<title>Your Page Title</title>
<!-- Your other head elements go here -->
</head>
<body>
<!-- Your page content goes here -->
</body>
</html>
  • og:title: Specifies the title of your content.
  • og:description: Provides a brief description of your content.
  • og:image: Specifies the URL of an image representing your content.
  • og:url: The canonical URL of your page.
  • og:type: Indicates the type of your content (e.g., "website" for a general webpage).

Twitter Card Setup

OGP Content on Twitter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Open Graph Protocol meta tags for Facebook -->
<!-- important ones -->
<meta property="og:title" content="Your Page Title">
<meta property="og:description" content="Brief description of your content">
<meta property="og:image" content="URL to your featured image">
<meta property="og:url" content="URL of the page">
<!-- optional ones -->
<meta property="og:type" content="website">

<!-- Twitter Card meta tags for Twitter -->
<!-- important ones -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Your Page Title">
<meta name="twitter:description" content="Brief description of your content">
<meta name="twitter:image" content="URL to your featured image">
<!-- optional ones -->
<meta name="twitter:url" content="URL of the page">

<!-- Add more tags as needed -->

<title>Your Page Title</title>
<!-- Your other head elements go here -->
</head>
<body>
<!-- Your page content goes here -->
</body>
</html>
  • twitter:card: Specifies the type of Twitter card to use. summary_large_image is recommended for websites with large images. summary also is an acceptable value.
  • twitter:title: Specifies the title of your content on Twitter.
  • twitter:description: Provides a brief description of your content for Twitter.
  • twitter:image: Specifies the URL of an image representing your content on Twitter.
  • twitter:url: The canonical URL of your page on Twitter.
  • twitter:site: Your Twitter username.

Adding a robots.txt file

To control how search engines index your site, you can create a robots.txt file in the root directory of your website. This file gives instructions to web crawlers about which pages or sections of your site should not be crawled or indexed.

Here’s a basic example of a robots.txt file:

User-agent: *
Disallow: /private/
Disallow: /app/

In this example, all web crawlers are allowed access to all parts of the site except the /private/ and /app/ directories.

Remember to customize the robots.txt file based on your specific requirements and site structure.

Conclusion

By incorporating Open Graph Protocol meta tags, along with the addition of a robots.txt file, you ensure better control over how your content appears on social media and how search engines index your website. This comprehensive approach contributes to a more effective online presence.

Resources

--

--

Zafir Sk Heerah
Zafir Sk Heerah

Written by Zafir Sk Heerah

Software Engineer | Consultant | Android and iOS Development | www.zfir.dev | blog.zfir.dev

No responses yet