Control How Your Drupal Content Looks on Facebook and X Shares
Social shares can significantly increase your brand exposure, expand your reach, boost your Drupal website traffic, and benefit your SEO. Of course, all this works much better if your shared content looks attractive, relevant, and well-formatted on social media.
Content preview snippets are built automatically when someone shares a link on social media. They include a title, a description, an image, or other information. Can you control how they look and make them so irresistibly clickable? Yes, sure, you can do this by setting up Drupal meta tags.
Meta tags are snippets of HTML that communicate your Drupal page’s key details to search engines and social platforms. Beyond basic SEO tags like meta descriptions, configuring your Drupal Open Graph meta tags defines how your content appears on networks like Facebook and LinkedIn, while setting up your Drupal Twitter Card tags performs the same job specifically for X (formerly Twitter).
In today’s post, we will give you a step-by-step guide to configuring how your Drupal website’s content appears on Facebook and X shares. We will be generating the Facebook Open Graph and the Twitter Card meta tags on a Drupal site using the Metatag module.
What are Open Graph and Twitter Card meta tags in Drupal?
Facebook Open Graph and Twitter Cards are protocols that define the use of structured metadata (aka meta tags) for displaying shared content on Facebook and X, respectively. Each of the social networks crawls the link that a user is sharing and searches for the appropriate metadata in order to build up a rich snippet.
Their primary role in social previews is to dictate the exact title, description, and thumbnail image displayed in user feeds, preventing platforms from pulling random page content. Together, they give you full control over your content’s visual identity in user feeds, directly influencing click-through rates and brand perception.
Facebook introduced its Open Graph protocol in 2010 to improve the display of content from third-party websites in its news feed. Open Graph includes a set of HTML tags. By adding Open Graph tags to your website’s code, you can control which elements of your content are displayed in the Facebook feed (or Messenger) and how.
Facebook Open Graph tags have later been adopted by other social networks, for example, LinkedIn or Pinterest. X created its own version of social meta tags and named it Twitter Cards. They work in a similar way to Open Graph tags. Moreover, in the absence of its special tags, X uses the Facebook OG tags.
You could add Open Graph and Twitter Card meta tags into the HTML code of your web pages, but things are much more editor-friendly if your website is built on a CMS like Drupal. Drupal handles these tags dynamically, automatically rendering the appropriate HTML whenever a page is created, updated, or shared.
At the heart of this process is the contributed Metatag module, which manages every aspect of social media creation. It provides intuitive user interfaces for both automatic tag generation and manual creation. Let’s now move on to the practical part and see how this works in Drupal.
Generating Open Graph and Twitter Card meta tags in Drupal
The Metatag module for Drupal streamlines social metadata management by automatically populating and rendering key meta tags directly from your website’s content fields. It relies on a system of dynamic placeholder tokens (such as [node:title] or [node:field_image]) that extract field data from your pages and map them into HTML standard formats.
Through dedicated submodules (Metatag Open Graph and Metatag Twitter Cards), it allows you to configure essential Open Graph tags, including og:title, og:description, og:image, and og:url, as well as X (Twitter) -specific tags like twitter:card, twitter:title, twitter:description, and twitter:image.
By defining these token mappings once as global or content-type defaults, Drupal automatically applies them site-wide while still giving editors the flexibility to override metadata on individual pages.
Module installation
Metatag is a complex Drupal module that takes care of all kinds of meta tags on Drupal websites. As we download it, we can see it comes packed with a bunch of submodules for search engines, various social networks, etc. Specifically for generating Facebook Open Graph and Twitter Card tags, we do not need all the submodules enabled.
What we will need is the main Metatag module, the Metatag Open Graph submodule, and the Metatag Twitter Cards submodule. These are the modules we need to check on the Extend tab of the Drupal admin dashboard and click “Save” at the bottom of the page.
The Metatag module will not be enabled without another Drupal module it depends on — Token, so we need to have Token installed first. It is used on many Drupal websites, so chances are you already have it. If you are using Composer for installing Drupal modules, it will take care of all dependencies, so there is no need to worry about Token in this case.
Basic configuration
Once the modules are installed, we can go to Configuration > Search and metadata > Metatag on our Drupal admin dashboard. This page shows a set of configurable meta tag patterns. They are passed from one level to the next but can be overridden at every more specific level.
For example, if we are interested in the meta tags for the blogs, these will be inherited from the existing “Content” and “Global” sections by default. But we can create a specific section that will be used for the blog only and populate it with blog-specific tags. That’s what we are going to do right now. Let’s click “Add Default Meta Tags.”
The dropdown menu will ask us to select which entity type we want to generate meta tags for. In our example, we select “Blog.”
We can also see the meta tag sections that include Open Graph and the Twitter Cards. The best decision is to leave them as-is for now and click “Save” at the bottom of the page. Why don’t we just start creating them right away? By saving the content type first, we make sure the “Browse available tokens” option displays more specific tokens, which will greatly simplify our further work.
A note on using Drupal tokens
For most of the meta tag fields, we will use tokens, which means the meta tag values will automatically change for every blog item. For example, instead of putting a specific node URL into the field, we will use the [node:url] token, and the smart Drupal will bring in the URL of the current node.
By clicking “Browse available tokens” at the top of the meta tag configuration page and expanding the “Nodes” section there, we can find the necessary tokens. A click on the selected token will put it directly into the meta tag field we have our cursor on. Alternatively, we can copy and paste the tokens into the fields.
Setting up Drupal Open Graph tags
In Configuration > Search and metadata > Metatag, we click “Edit” under “Operations” for “Content: Blog,” then we find and expand the Open Graph section.
We immediately see a long list of fields for the Open Graph meta tags. There is no need to fill out every single field. Here are some of the most important ones:
- Content type. This is not the content type as we know it in Drupal but the content type for Facebook. Let’s put “blog” in this field.
- URL. After checking “Browse available tokens,” let’s put in the [node:url] token.
- Title. Let’s put in the [node:title] token.
- Description. Let’s put in the [node:summary], which provides a truncated version of the node’s body.
- Image. Let’s put in the [node:field_blog_image] token. It is named after the Blog image field that we have for the Blog and can be different in your case. Anyway, we need to make sure we are using a token for the field that represents the blog cover image. If necessary, we can always check how this field is called in Structure > Content types > *Blog* > Manage fields.
Setting up Open Graph meta tags in Drupal
https://drive.google.com/file/d/1oWzdQGEmQQOYdmhTugL_9rsDeDbnR42o/view?usp=drive_link
After we click “Save,” it’s best practice to clear our Drupal website’s caches, otherwise, the changes might not apply. Caches can be cleared in Configuration > Performance > Clear all caches.
Next, we should be able to see our Open Graph meta tags in the source code of every blog node. Let’s open a node, right-click it and select “View page source,” or just click F12. Yes, the OG tags are there as we expand the <head> section:
- “og:type”
- “og:url”
- “og:title”
- “og:description”
- “og:image”
We can check the link in “og:image” to make sure it really leads to our image. Yes, perfect!
Checking the source code of a Drupal content item for Open Graph meta tags
https://drive.google.com/file/d/1reNxbl42doY2vAse9rP8oQM6bvV8OYgI/view?usp=drive_link
We can also put the page’s URL into the Facebook Sharing Debugger. You will find the link to this tool directly in the Open Graph section.
It allows us to preview how our shared content will look on Facebook and to debug any issues in case they are found. We just put the node’s URL into the debugger and click “Debug.” The page needs to be publicly available for Facebook to be able to “scrape” it. If we make any changes to the meta tags, we can click “Scrape again” in the Debugger, and the updated version will be there.
Setting up Drupal’s Twitter Card tags
The setup of the Twitter Card meta tags for X is pretty similar to that of Facebook Open Graph meta tags. In Configuration > Search and metadata > Metatag, we click “Edit” under “Operations” for “Content: Blog,” then find and expand the X Card section, titled ‘Twitter Card’.
- Twitter card type. Let’s select “Summary card with large image.”
- Description. Similar to how we did with Open Graph, we can use the “Browse available tokens” option and select the [node:summary] token.
- Title. Let’s put in the [node:title] token.
- Site’s Twitter (X) account: @YourSiteHandle (optional)
- Image URL. Let’s put in the [node:field_blog_image] token.
- Image alternative text. Let’s expand our Blog Image section and grab the [node:field_blog_image:alt] token.
Title, Description, and Image are optional. If left blank, X will automatically fall back to og:title, og:description, and og:image.
Setting up Twitter Card meta tags in Drupal
https://drive.google.com/file/d/1z1zbUEIXnrxo1Rxlrw9dMyxDto9pYIrh/view?usp=drive_link
Similar to the workflow with Facebook Open Graph, we can open a node and view its source code to check if the Twitter Card meta tags are there. If something is not updated, the cache clearing will come for help. After that, we can validate the Twitter Card meta tags using the Card Validator by simply entering the page URL.
Open Graph and Twitter Cards for individual Drupal content
To give content editors control of the preview of specific Drupal nodes, it also makes sense to add meta tags directly into the content admin dashboard. We can do it by going to Structure > Content types > *Blog* > Manage fields > Add field, and add a new Meta tag field to the blog content type.
Adding a meta tag field to the content admin dashboard
https://drive.google.com/file/d/1KV2cq2_yq0pdlO7mOsnf7AVjtxJAWecQ/view?usp=drive_link
With this done, the collapsible Meta tag section group will become available on the right-hand side of each node’s edit page. The sections are Basic tags, Advanced, Open Graph, and Twitter Cards (or more — depending on the Meta tag submodules you have installed).
However, the meta tags on the node’s edit page are only used for individual Drupal nodes, so if we want a comprehensive default setup for the Blog or another Drupal content type, we need to go to the Configuration page for the Metatag module and use the previously described workflow.
Facebook Open Graph and Twitter Card meta tags are just examples of what you can do using the Metatag module in Drupal. In addition, Drupal has thousands of other useful tools that will help you easily reach various goals on your website. Have questions, comments, or ideas? Talk to our Drupal team!