Add Meta Title, Meta Keyword and Meta Description In Custom Module

Add Meta Title, Meta Keyword and Meta Description In Custom Module

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add Meta Title, Meta Keyword and Meta Description In Custom Module

Adding Meta Title, Keywords, and Description to a Custom Magento Module

Meta tags are crucial for SEO (Search Engine Optimization) as they help search engines understand what your pages are about. In Magento, you can add meta titles, keywords, and descriptions to your custom modules using the following methods.

Understanding the Meta Tag Structure

Before we dive into the code examples, let’s first understand how meta tags work in HTML:

<meta name="keywords" content="keyword1, keyword2, keyword3">

In this example, name is the attribute that defines what type of information the tag contains. The possible values for name include “description”, “keywords”, and “viewport”. The content attribute holds the actual value of your meta tag.

Adding Meta Tags in the Layout XML File

The most common approach to adding meta tags in Magento is by using the layout XML file. Here’s how you can do it:

  1. Create a new layout XML file under your custom module directory, e.g., app/code/Vendor/Module/view/frontend/layout/some_layout.xml.

  2. Add the following code inside the <page> tag:

<!-- Meta Title -->
<referenceContainer name="head">
    <block class="Magento\Framework\View\Element\Template" template="Vendor_Module::meta.phtml"/>
</referenceContainer>
  1. Create a new PHTML file named meta.phtml in your custom module’s view directory:
<!-- Meta Title -->
<?php echo $block->getLayout()->createBlock('Magento\Framework\View\Element\Template')->setTemplate('Magento_Theme::html/head/meta.phtml')->toHtml(); ?>

<!-- Meta Keywords and Description -->
<?php if ($keywords = $block->getData('keywords')): ?>
    <meta name="keywords" content="<?php echo $this->escapeHtml($keywords); ?>"/>
<?php endif; ?>

<?php if ($description = $block->getData('description')): ?>
    <meta name="description" content="<?php echo $this->escapeHtml($description); ?>"/>
<?php endif; ?>
  1. Now, you can set the meta data in your controller or block using $block->setData('keywords', 'your_keywords'); and $block->setData('description', 'your_description');.

Remember to replace 'Vendor' with your actual vendor name in the XML file path.

Adding Meta Tags Programmatically

If you prefer a more dynamic approach, you can add meta tags programmatically using the Magento\Framework\View\Element\Template class. Here’s an example:

public function getMetaTags() {
    $keywords = 'your_keywords';
    $description = 'your_description';

    $metaData = [
        'keywords' => $keywords,
        'description' => $description,
        'viewport' => 'width=device-width, initial-scale=1',
    ];

    return $this->helper('Magento\Framework\View\Helper\Head')->getMetaKeywords($metaData);
}

You can then call this function in your PHTML file to display the meta tags:

<?php echo $block->getMetaTags(); ?>

Conclusion

Adding meta titles, keywords, and descriptions to your custom Magento modules is a straightforward process. Whether you choose the layout XML approach or the programming approach, both methods effectively add these essential SEO elements to your pages.

Remember to keep your meta tags concise and relevant to improve your visibility in search engine results. For more advanced SEO techniques, consider exploring our guide on Advanced SEO Techniques for Magento.

Happy coding!

Performance Extensions Themes SEO

Our network