4

I'm currently developing some Magento2 themes. I noticed that in Magento2 Meta Keywords are still added. I want to remove this meta keywords from all pages, does anyone know the proper way to achieve this?

I did find meta.phtml, but I think this file is not used on a product page. Also the file Product/View.php adds the keywords with this function:

$this->pageConfig->setKeywords($keyword);

but I don't think it would be the prober way to extend the _prepareLayout function, right?

Thanks.

2

3 Answers 3

11

I just had to do the same and decided to just add a plugin after the getKeywords Method of the page config which returns an empty string. works great

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\Module\Plugin\PageConfig\RemoveMetaKeywords" sortOrder="9999"/>
    </type>
</config>

Plugin Class:

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

class RemoveMetaKeywords
{
    public function afterGetKeywords($subject, string $return)
    {
        return '';
    }
}
4
  • and the entire <meta name="keywords"... is gone. :) Thanks
    – JustinP
    Commented Feb 6, 2019 at 23:22
  • I get some error on some pages..
    – ozan yeni
    Commented Jan 16, 2020 at 12:37
  • Fatal error: Uncaught TypeError: Argument 2 passed to Learning\FirstUnit\Plugin\PageConfig\RemoveMetaKeywords::afterGetKeywords() must be of the type string, null given, called in
    – ozan yeni
    Commented Jan 16, 2020 at 12:38
  • Change class to: public function afterGetKeywords($subject, $return) { return (string)null; } and the error is gone.
    – Mike
    Commented Mar 21, 2021 at 12:43
1

Easy to remove or edit default meta keywords at admin configuration:

Store > Configuration > Design [General] - HTML Head
1
  • 1
    I want it removed from all pages, so also the category and product pages. When you create a product, the meta keywords will be automatically filled and added to the product page. So changing the default meta keywords will not do the trick.
    – Silvan
    Commented Jan 6, 2016 at 7:12
0

Just override the Magento\Framework\View\Page\Config\Renderer

As this class's the method renderMetadata responsible for meta keywords render. On the override class's at that method add this condition:

 // add this code for stop render of keywords
    if ($name = 'keywords') {
        continue;
    }

Override function may be look

public function renderMetadata()
    {
        $result = '';
        foreach ($this->pageConfig->getMetadata() as $name => $content) {
            $metadataTemplate = $this->getMetadataTemplate($name);
            if (!$metadataTemplate) {
                continue;
            }
            // add this code for stop render of robot
            if ($name == 'keywords') {
                continue;
            }
            $content = $this->processMetadataContent($name, $content);
            if ($content) {
                $result .= str_replace(['%name', '%content'], [$name, $content], $metadataTemplate);
            }
        }
        return $result;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.