1

I need to remove meta title from header in all pages.

<meta name="keywords" content="Oneplus One"/>
<meta name="robots" content="NOINDEX,NOFOLLOW"/>
<!-- I need to remove meta title -->
<meta name="title" content="Oneplus One"/>
<meta name="viewport" content="width=device-width/>
<meta name="format-detection" content="telephone=no"/>
<title>Oneplus One</title>

I have created module RemoveMetaTitle.

di.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Page\Config">
        <plugin name="unsetMetaTitle" type="Vendor\RemoveMetaTitle\Plugin\PageConfig\UnsetMetaTitle" sortOrder="9999"/>
    </type>
</config>

Plugin/PageConfig/UnsetMetaTitle.php :

namespace Vendor\RemoveMetaTitle\Plugin\PageConfig;

class UnsetMetaTitle
{
    public function afterGetMetaTitle($subject, $return)
    {
        return '';
    }
}

afterGetMetaTitle seems to be not even executed.

1

di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Page\Config">
        <plugin name="unsetMetaTitle" type="Vendor\Module\Plugin\PageConfig\unsetMetaTitle" sortOrder="9999"/>
    </type>
</config>

PageConfig class

<?php
declare(strict_types = 1);
namespace Vendor\Module\Plugin\PageConfig;

class unsetMetaTitle
{
    public function afterGetMetaTitle($subject, string $return)
    {
        return '';
    }
}
| improve this answer | |
  • This class is not even executed. Are you sure the plugin name "unsetMetaTitle" is correct? Because I tried "removeMetaKeywords", the class is executed. – Vinoth S Mar 14 '19 at 6:17
  • any plugin name will work. so 'unsetMetaTitle' is correct one. – sandip Mar 14 '19 at 6:23
  • I tried to add function "afterGetKeywords", it is executed. But "afterGetMetaTitle" is not executed. – Vinoth S Mar 14 '19 at 6:48
  • Are you sure it is "afterGetMetaTitle"? – Vinoth S Mar 14 '19 at 9:00
  • share your code in question – sandip Mar 14 '19 at 9:15
0

Just create a new module for this:

add below code in di.xml:

<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Page\Config">
    <plugin name="removeMetaKeywords" type="Vendor\Namespace\Plugin\PageConfig\RemoveMetaKeywords" sortOrder="9999"/>
</type>

add below code in Vendor\Namespace\Plugin\PageConfig\RemoveMetaKeywords

<?php
    declare(strict_types = 1);
    namespace Vendor\Module\Plugin\PageConfig;
    class RemoveMetaKeywords
    {
       public function afterGetKeywords($subject, string $return)
       {
         return '';
       }
    }
 ?>

and it will remove the entire <meta name="keywords" /> from all the pages.

| improve this answer | |
  • Thanks, but I was looking to remove title meta. – Vinoth S Mar 14 '19 at 6:19
  • can you set custom meta title from here? – jibin george Nov 6 '19 at 12:57
0

I struggled with the same problem which I fixed by creating a custom module.

The title meta tag is being generated from magento framework. You can override the function by creating a custom module like I did.

  1. /app/code/Mediajedi/CustomHead/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Mediajedi_CustomHead" setup_version="1.0.0">
    </module>
</config>
  1. /app/code/Mediajedi/CustomHead/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mediajedi_CustomHead',
    __DIR__
);
  1. /app/code/Mediajedi/CustomHead/etc/di.xml
<config>
    <preference for="Magento\Framework\View\Page\Config\Renderer" type="Mediajedi\CustomHead\View\Page\Config\AfCustomrenderer" />
</config>
  1. /app/code/Mediajedi/CustomHead/View/Page/Config/AfCustomrenderer.php
<?php

namespace Mediajedi\CustomHead\View\Page\Config;

use Magento\Framework\View\Page\Config;

class AfCustomrenderer extends \Magento\Framework\View\Page\Config\Renderer
{
    /**
     * Returns metadata template
     *
     * @param string $name
     * @return bool|string
     */
    protected function getMetadataTemplate($name)
    {
        if (strpos($name, 'og:') === 0) {
            return '<meta property="' . $name . '" content="%content"/>' . "\n";
        }

        switch ($name) {
            case Config::META_CHARSET:
                $metadataTemplate = '<meta charset="%content"/>' . "\n";
                break;

            case Config::META_CONTENT_TYPE:
                $metadataTemplate = '<meta http-equiv="Content-Type" content="%content"/>' . "\n";
                break;

            case Config::META_X_UI_COMPATIBLE:
                $metadataTemplate = '<meta http-equiv="X-UA-Compatible" content="%content"/>' . "\n";
                break;

            case Config::META_MEDIA_TYPE:
                $metadataTemplate = false;
                break;

            default:
              if($name != 'title'){
                $metadataTemplate = '<meta name="%name" content="%content"/>' . "\n";
              } else{
                  $metadataTemplate = '';
              }
                break;
        }
        return $metadataTemplate;
    }
}

You can see in the default case in AfCustomrenderer.php file, we are checking for title meta tag and assigning an empty value to $metadataTemplate which returns an empty value and results in removing of the title meta tag from the head.

Please note that "Mediajedi" is the vendor name and "CustomHead" is our module name. You can change them to yours.

| improve this answer | |

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.