June, 2016

By In Magento

Meta Tags for custom module in Magento

While Magento provide method to set page title and description in block file using commands, in some cases that is not enough and additional meta tags need to be defined for particular page to improve site usability and SEO.

$this->getLayout()->getBlock('head')->setTitle('page title');
$this->getLayout()->getBlock('head')->setDescription('page description');


Additional meta tags are needed in case that module has it’s own page and customers want to shear some information from that page. Without properly set meta tags generic content will be used and sheared link will be displayed with desired data, most common issue is wrong image, page title and description.

There are several possible methods to add meta tags to custom module page, we will use one that require minimal skills and can be quickly implemented into any module. With this method you will not edit any file outside of your custom module and you can easily set tags for various social media using code provided in demo examples. On top of that you can set separate file for every module section and with some additional programming it is possible to add multi-language support.

Find your custom module template folder and create a new PHTML file, we will name it ‘metatags.phtml‘ to be easily recognized when you return later to your module. Open file in editor and simply copy example code from social media for which you want to set tags. To name few common OG tag examples that are:

<meta property="og:title" content="Custom page title"/>
<meta property="og:image" content="http://www.domain.com/skin/base/default/images/logo.png"/>
<meta property="og:site_name" content="Custom site name"/>
<meta property="og:description" content="Custom page description" />
<meta property="og:url" content="http://www.domain.com/custom-module.html" />


Replace content values with one that you want and save file.

To include meta tags to appear on front end we will need to edit module layout file, Open XML file in editor and add line that will include meta-tags template into page head section.

<block type="core/template" name="custom_module_meta" template="custom_module/metatags.phtml" />

Complete code should look like this:
<reference name="head">
<block type="core/template" name="custom_module_meta" template="custom_module/metatags.phtml" />

After saving file, refresh Magento cache and reload page of your custom module. To check are tags displayed properly open page in ‘View Source’ mode, search for head section and you should see all meta tags that you defined for your custom module. If you want to additionally check results you can use social media tools that will fetch page for you and render all information that are recognized, also in case that there are errors they will be marked (Facebook has nice tool here). Now when you shear content from page of your custom module proper data will be used.

Read more