How to set a Meta robot values for Custom extension page in Magento 2

How to set a Meta robot values for Custom extension page in Magento 2

Beside of selling products online, the store owner still has to manage and take care of each and every aspect that helps to boost store sales and ranking over SERP pages. Securing your position in the top 5 ranking of Search engine pages drastically helps, but it requires a lot of efforts to build links, content, Meta’s, and everything that matters. Even the small start helps to achieve big, considering Magento comes with preloaded features of SEO options to set separate Meta values for each product, category pages, manage sitemap and much more that makes first pick CMS for the store owner. But what about custom extension pages?

Till today, we have developed more than 100+ custom Magento extensions to fulfill each client needs. Recently, while creating custom pages within an extension we came across the requirement of set-n-up custom Meta robot value for not indexing pages in search page results. You can do it too, either by using ‘layout.xml’ or using ‘Controller Layout Block’ by following the below steps.

1. Using Layout XML of the page :
In this method, you need to put below code into your Magento custom page layout file.
I.e. : app\code\Vendor\Extension\view\frontend\layout\vendor_extension_file.xml

<pre class="lang:default decode:true">
<head>
 <meta name="robots" content="NOINDEX,NOFOLLOW"/>
</head>
</pre>

2. Using Controller Layout Block:
For this method, you have to add following code inside your controller file from which all your Magento custom pages will be loaded.
I.e. : app\code\Vendor\Extension\Controller\Extension\Action.php

<pre class="lang:default decode:true">
<?php
namespace Vendor\ Extension\Controller\Extension;

class Action extends \Magento\Framework\App\Action\Action
{
    protected $_pageConfig;

   public function __construct(\Magento\Framework\View\Page\Config $pageConfig)
   {
      $this->_pageConfig = $pageConfig;
   }

   public function execute()
   {
      $this->_pageConfig->setRobots('INDEX,FOLLOW');
   }

}
</pre>

Also, you can play with these codes according to your need of setting up custom robot value for custom Magento extension pages.

Lastly, hit the below stars if this code works for you and comment down below if you face any issue while implementing this code inside your extension.

Happy Coding!

Previous Article

How to Create Dynamic File while installing Extensions in Magento 2

Next Article

How to fix an error while uploading a logo from admin in Latest Magento 2.2.4

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨