I want to add of tags for facebook social media in Magento 2. So I do not know by where I can add this from code. Please help me to figure out this.

  • u can check in file vendor\magento\module-catalog\view\frontend\templates\product\view\opengraph/general.phtml it is already set. Is it appearing on the website browser source? – Amrit Pal Singh Oct 23 at 10:20
  • Do you want to add og tags for all pages ? – Kishan Savaliya Oct 23 at 10:27
  • @AmritPalSingh: But this is only working for catalog product detail pages. I want to add on homepage and other cms pages – Shashank Sharma Oct 23 at 10:27
  • @ShashankSharma, Please review my answer. – Kishan Savaliya Oct 23 at 10:31

You can add one block in cms_index_index.xml file in your custom theme and you can add your OG tags there.


Content for this file is ..

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <attribute name="prefix" value="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# product: http://ogp.me/ns/product#" />
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="opengraph.general" template="Magento_Theme::html/header/meta.phtml"/>

You can create template here..


Content for this file is..

    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $request = $_objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
<?php if($request->getFullActionName() == 'cms_index_index') : ?>
    <meta property="og:type" content="test" />
    <meta property="og:title" content="test" />
    <meta property="og:description" content="test" />
    <meta property="og:url" content="test" />
    <meta property="og:image" content="test" />
<?php endif; ?>

Hope this will help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.