Custom Open graph tags for Magento head.phtml

<?php
$product = Mage::registry('current_product');
if($product):
?>
<meta property="og:title" content="<?php echo trim($product->getName()); ?>"/>
<meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>"/>
<meta property="og:type" content="product"/>
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(100,100);?>"/>
<?php endif; ?>

<?php if ($this->getIsHomePage()):?>
    <meta property="og:title" content="<?php echo ($this->getForcedTitle()) ? $this->getForcedTitle() : $this->getTitle() ?>"/>
    <meta property="og:type" content="website"/>
    <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>"/>
<?php else: ?>
	<?php if($this->getForcedOgTitle()): ?>
    <meta property="og:title" content="<?php echo trim($this->getForcedOgTitle()); ?>"/>
    <?php endif; ?>
    <?php if($this->getForcedOgType()): ?>
    <meta property="og:type" content="<?php echo trim($this->getForcedOgType()); ?>"/>
    <?php endif; ?>
    <?php if($this->getForcedOgImage()): ?>
    <meta property="og:image" content="<?php echo trim($this->getForcedOgImage()); ?>"/>
    <?php endif; ?>
    <?php if($this->getForcedOgImage() || $this->getForcedOgImage() || $this->getForcedOgType() || $this->getForcedOgTitle()): ?>
    <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>"/>
    <?php endif; ?>
<?php endif; ?>

Then in layout or controllers do this:

$head = $this->getLayout()->getBlock('head');
$head->setForcedTitle($title);
$head->setForcedOgTitle($title);

One Reply to “Custom Open graph tags for Magento head.phtml”

  1. Am i missing something here as I am not able to find setForcedOgTitle function in defaut magento installation.

Comments are closed.