Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Add Meta Tags to Header in Magento 2

Vinh Jacker | 12-18-2024

Add Meta Tags to Header

As you might already know, the HTML <head> section of the page includes different meta tags, CSS, JS files definition, JS code’s pieces, etc. Normally, no complex logic is needed to add some proper content there. However, in various situations, we will need to insert several elements which depend on the configuration of the system into the head section.

If you have been working with Magento 1, you will find out that it’s easy to operate with the contents of the head section. However, in the newest Magento version, adding meta tags to the header might be a bit tricky for you. Because of that, in today’s post, I will guide you on how to add blocks and templates, more specifically how to Add Meta Tags to the head section in Magento 2.

Magento SEO Services
by Mageplaza

Let experienced professionals optimize your website's ranking

Learn more
Magento SEO service

Add Meta Tags to Header

In order to add <meta> tags to your layout’s <head> element, you need to create a theme extending file which is similar to this: app/design/frontend/<Vendor>/<theme>/Magento_Theme/layout/default_head_blocks.xml.

By default, \Magento\Framework\View\Page\Config\Renderer is the class by which the <meta> tags are rendered by. There are five types of meta, and a catch-all (the default) is rendered by this class:

  • og
  • charset
  • content_type
  • x_ua_compatible
  • media_type
  • “default” case

You can use the following as an examples to include in your layout themes:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <head>
    <!-- This will create a tag like '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">' -->
    <meta name="x_ua_compatible" content="IE=edge,chrome=1"/>
    <!-- This will create a tag like '<meta property="og:type" content="article"/>'' -->
    <meta name="og:type" content="article"/>
    <!-- This will create a tag like '<meta charset="UTF-8">' -->
    <meta name="charset" content="UTF-8"/>
    <!-- This will create a tag like '<meta http-equiv="Content-Type" content="content-type-value"/>' -->
    <meta name="content_type" content="content-type-value"/>
    <!-- This tag will not render (see \Magento\Framework\View\Page\Config\Renderer for details) -->
    <meta name="media_type" content="any-value"/>
    <!-- This will create a tag like '<meta name="my_custom_type" content="my_custom_value"/>' -->
    <meta name="my_custom_type" content="my_custom_value"/>
   </head>
</page>

Conclusion

Above I have just provided you the method to add meta tags to header in Magento 2. I hope it is helpful for you. Should you have any questions or new ideas, feel free to leave a comment below.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services