Per product meta robots tag control in Magento

products-meta-robots-admin

My first Magento related task here at Inchoo was to assist our client to gain control over his meta robots tag on per product basis. In this article I’ll present simple Magento extension designed to do just that. I’ll also show you how to create custom product attribute programmatically trough setup resource installer script and how to retrieve this attribute later on.

First things first, let’s take a look at the new functionality on Product Information screen in Magento backend:

Per product meta robots

As you can see this code adds a new dropdown labeled Meta Robots to Meta Information tab allowing us to manually configure meta robots tag for each product.

Magento extension at hand is available from its GitHub repository page, here I’ll just include a few important code snippets. First important thing is our setup class from Inchoo_ProductMetaRobots/app/code/community/Inchoo/ProductMetaRobots/Model/Resource/Setup.php file:

<?php
 
class Inchoo_ProductMetaRobots_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{
    /**
     * Add our custom attributes
     *
     * @return Mage_Eav_Model_Entity_Setup
     */
    public function installCustomProductAttributes()
    {
        $attributes = $this->_getCustomProductAttributes();
 
        foreach ($attributes as $code => $attr) {
            $this->addAttribute('catalog_product', $code, $attr);
        }
 
        return $this;
    }
 
    /**
     * Remove custom attributes
     *
     * @return Mage_Eav_Model_Entity_Setup
     */
    public function removeCustomProductAttributes()
    {
        $attributes = $this->_getCustomProductAttributes();
 
        foreach ($attributes as $code => $attr) {
            $this->removeAttribute('catalog_product', $code);
        }
 
        return $this;
    }
 
    /**
     * Returns entities array to be used by
     * Mage_Eav_Model_Entity_Setup::installEntities()
     *
     * @return array Custom entities
     */
    protected function _getCustomProductAttributes()
    {
        return array(
            'inchoo_meta_robots' => array(
                'group'             => 'Meta Information',
                'label'             => 'Meta Robots',
                'type'              => 'varchar',
                'input'             => 'select',
                'default'           => '',
                'class'             => '',
                'backend'           => '',
                'frontend'          => '',
                'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                'visible'           => true,
                'required'          => false,
                'user_defined'      => false,
                'searchable'        => false,
                'filterable'        => false,
                'comparable'        => false,
                'visible_on_front'  => false,
                'visible_in_advanced_search' => false,
                'unique'            => false,
                'option' => array (
                    'value' => array(
                        'INDEX,FOLLOW' => array(0=>'INDEX,FOLLOW'),
                        'NOINDEX,FOLLOW' => array(0=>'NOINDEX,FOLLOW'),
                        'NOINDEX,NOFOLLOW' => array(0=>'NOINDEX,NOFOLLOW'),
                        'INDEX,NOFOLLOW' => array(0=>'INDEX,NOFOLLOW')
                    )
                )
            )
        );
    }
}

Inside _getCustomProductAttributes() function we can place new product attributes. In our case there’s just one attribute with ‘inchoo_meta_robots’ code. This attribute will be used to construct dropdown menu (‘input’ with value ‘select’) with four options specified by the ‘option’ array. This menu will be configurable at Product Information -> Meta Information -> Meta Robots as specified by the ‘group’ and ‘label’ array elements. Inside our setup class we also have two public functions, first one installCustomProductAttributes() is used for creating our custom product attributes and the second removeCustomProductAttributes() for removing them if necessary. These two functions are available inside our install script located at Inchoo_ProductMetaRobots/app/code/community/Inchoo/ProductMetaRobots/sql/inchoo_productmetarobots_setup/install-0.0.1.php. Here’s the code you’ll find there:

<?php
 
$installer = $this;
 
// Install our custom attributes
$installer->installCustomProductAttributes();
 
// Remove our custom attributes
//$installer->removeCustomProductAttributes();

By default install script is adding custom product returned by _getCustomProductAttributes(). If you encounter any difficulties, the removal code is there to assist. If this happens also don’t forget to delete the ‘inchoo_productmetarobots_setup’ row from ‘core_resource’ table to trigger the install script again.

All that’s left to do is to rewrite Mage_Page_Block_Html_Head block using something like following inside global section of your config.xml:

<config>
    <global>
        <blocks>
            <page>
                <rewrite>
                    <html_head>Inchoo_ProductMetaRobots_Block_Page_Html_Head</html_head>
                </rewrite>
            </page>
        </blocks>
    </global>
</config>

In our code we override Mage_Page_Block_Html::getRobots() function to inject our custom product attribute inside robots meta tag for product page view. The code is located at Inchoo_ProductMetaRobots/app/code/community/Inchoo/ProductMetaRobots/Block/Page/Html/Head.php and here’s the contents of that file:

<?php
 
class Inchoo_ProductMetaRobots_Block_Page_Html_Head extends Mage_Page_Block_Html_Head
{
    /**
     * Customize meta robots tags when viewing product.
     *
     * @return string
     */
    public function getRobots()
    {
        parent::getRobots();
 
        if (($_product = Mage::registry('current_product')) &&
                ($robots = $_product->getAttributeText('inchoo_meta_robots'))) {
            $this->_data['robots'] = $robots;
        }
 
        return $this->_data['robots'];
    }
}

We simply call parent::getRobots() for the default behavior, and then override it if we are at the product page view, and if current product has meta robots value selected in its Meta Robots dropdown menu.

I hope you find this code snippets useful, if you have any suggestions feel free to leave your comment here or fork this code from its GitHub repository page.


16 comments

  1. I have installed the extension but can’t make it work. The dropdown is shown on the product pages, but when I change the status to i.e. Noindex, Follow, then it does not change on the site.

    Any ideas?

  2. Dear Marko Martinovic,
    Thanks for giving very important guide and package. Working fine. Showing Robots option in Meta information. Guys please follow all the major 5 Things which Marko Martinovic provided. We installed this package on Magento Community Edition, Version 1.9.1 and its working fine without any error.

    Thanks.

  3. Hi Marko,
    This module conflicts with http://inchoo.net/magento/how-to-add-external-javascript-css-file-to-magento/


    a:5:{i:0;s:231:"Invalid method Inchoo_ProductMetaRobots_Block_Page_Html_Head::addExternalItem(Array
    (
    [0] => external_js
    [1] => http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js
    [2] => Mage_Core_Model_Layout_Element Object
    (
    )

    )
    )";i:1;s:2144:"#0 [internal function]: Varien_Object->__call('addExternalItem', Array)
    #1 [internal function]: Inchoo_ProductMetaRobots_Block_Page_Html_Head->addExternalItem('external_js', 'http://yui.yaho...', Object(Mage_Core_Model_Layout_Element))
    #2 path-of-my-site\app\code\core\Mage\Core\Model\Layout.php(347): call_user_func_array(Array, Array)
    #3 path-of-my-site\app\code\core\Mage\Core\Model\Layout.php(213): Mage_Core_Model_Layout->_generateAction(Object(Mage_Core_Model_Layout_Element), Object(Mage_Core_Model_Layout_Element))
    #4 path-of-my-site\app\code\core\Mage\Core\Model\Layout.php(209): Mage_Core_Model_Layout->generateBlocks(Object(Mage_Core_Model_Layout_Element))
    #5 path-of-my-site\app\code\core\Mage\Core\Controller\Varien\Action.php(345): Mage_Core_Model_Layout->generateBlocks()
    #6 path-of-my-site\app\code\core\Mage\Cms\Helper\Page.php(110): Mage_Core_Controller_Varien_Action->generateLayoutBlocks()
    #7 path-of-my-site\app\code\core\Mage\Cms\Helper\Page.php(52): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_IndexController), 'home')
    #8 path-of-my-site\app\code\core\Mage\Cms\controllers\IndexController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_IndexController), 'home')
    #9 path-of-my-site\app\code\core\Mage\Core\Controller\Varien\Action.php(420): Mage_Cms_IndexController->indexAction()
    #10 path-of-my-site\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(253): Mage_Core_Controller_Varien_Action->dispatch('index')
    #11 path-of-my-site\app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #12 path-of-my-site\app\code\core\Mage\Core\Model\App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
    #13 path-of-my-site\app\Mage.php(627): Mage_Core_Model_App->run(Array)
    #14 path-of-my-site\index.php(82): Mage::run('', 'store')
    #15 {main}";s:3:"url";s:1:"/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}

  4. Is it possible? Put in meta tag description the product price discount. Just putting a global variable {{pricediscount}}on the product page of the block?

  5. Hi Marko,

    I wanted to ask you about the importance of the Description, Metw key words and Meta description when talking about working with array attribute managers. What should I write in each field? Thanks

  6. Hello, I have a question. We are making some changes on our website and I would like Google and other search engines not to index it for a while (URLs are chaning quite often now). Would it do any harm if the web has been indexed for a month now, I would turn indexing off, and after a while I would turn it on again?

  7. For my custom module i have created some extra fields in meta information tab for products.But multiselect box value is not getting saved.Unger getDefaultEntities() function in Setup.php i have added like follows :

    'meta_advanced'       	=> array(
                            'type'                       => 'text',
                            'label'                      => 'Meta Robots Advanced',
                            'input'                      => 'multiselect',
                            'required'                   => true,
                            'sort_order'                 => 12,
                            'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                            'group'                      => 'Meta Information',
    						'option'					=>array(
    							'values'	=>array(
    								'option1'	=>array(
    									'None'	=>	'None',
    									'NO ODP'	=>	'NO ODP',
    									'NO YDIR'	=>	'NO YDIR',
    									'NO Archive'	=>	'NO Archive',
    									'No Snippet'	=>	'No Snippet'
    								)								
    							)
    						)
                        ),
  8. Hi again,

    I am sorry for spamming) Probably, there is nothing wrong with the module, I am using another module provided by you – External JS, and it might conflict with this one, so I just added the getRobots() function to the Inchoo_Xternal_Block_Html_Head class and it is working now!

  9. Hello,

    Thank you for the module! However, it does not work in my store, well partially. I can set robots in admin panel however it is not showing up on product pages. I believe the class is not being extended in Head.php, I tried to dump a random variable trough the getRobots() function but it is not showing up either. Do you know what could be causing this?

    Thank you!

  10. @Dave:

    Hello,
    this article describes this functionality from Magento developer’s point of view so I didn’t include any screenshots initially. Even though instructions for accessing new functionality was included inside original article, I’ve added screenshot to make things clearer.

  11. let me change that last comment 🙂 I see you did cover my issue in the comments but you mention “Meta Robots dropdown where you can select desired option”

    what are those options? can’t you show a screenshot?

  12. this sounds exactly like what I need BUT you don’t show or mention how this actually works for the client in question? can you please explain what the client has to do to “gain control over his meta robots tag on per product basis” once your script is setup?

  13. How to setup this one in magento admin panel, I have followed this instruction below but i cant find the meta robots option
    This menu will be configurable at Product Information -> Meta Information -> Meta Robots as specified by the ‘group’ and ‘label’ array elements

    1. Hello Jesse,
      here are the exact steps to install code from Inchoo_ProductMetaRobots GitHub page:

      1. Download and extract Inchoo_ProductMetaRobots ZIP package.

      2. Inside Inchoo_ProductMetaRobots-master folder you will find app folder. You should copy this folder to your Magento root by merging it’s contents with the rest of Magento code, more precisely the existing app folder.

      3. Go to System -> Cache Management, click Select All and then Submit with Refresh action selected to clear Magento cache.

      4. Now go to your Magento admin -> Catalog -> Manage Products and select the product whose meta robots tag you want to control.

      5. Then go to Meta Information tab of your product and you will find Meta Robots dropdown where you can select desired option.

      If something didn’t go well just trace your steps to make sure your did everything correctly.

      I hope this helps!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.