Sunday, September 11, 2011

Magento: How to change Title, Sescription and Keywords in static pages

Magento is fairly SEO friendly but for certain pages, it does not allow you to change the Title, Description or the keywords. Some of these pages are very important for Google crawls such as Search terms, Advanced search, Contact us, Sitemaps etc. There are other pages such as accounts etc, which also set the default keywords, however I do not care if they are crawled by Google as they are customer specific.

You can set all of these by making simple changes the layout XML files and the block functional file.
1.
We have to create the functions setDescription and setKeywords in .../app/code/core/Mage/Page/Block/Html/Head.php  (Remember it always best to make a copy of this file in .../app/code/local/Mage/Page/Block/Html/Head.php).

    /**
     * Set description element text
     *
     * @param string $description
     * @return Mage_Page_Block_Html_Head
     */
    public function setDescription($description)
    {
        $this->_data['description'] = $description;
        return $this;
    }


    /**
     * Set Keywords element text
     *
     * @param string $keywords
     * @return Mage_Page_Block_Html_Head
     */
    public function setKeywords($keywords)
    {
        $this->_data['keywords'] = $keywords;
        return $this;
    }



2. 
Let us take Advanced Search for example and see how we can make these changes. In layout/catalogsearch.xml

 After <label>Advanced Search Form</label>
        
        <reference name="head">
        <action method="setForcedTitle"><title>%TITLE%Vyom search</title></action>
        <action method="setDescription"><description>%DESCRIPTION%</description></action>
        <action method="setKeywords"><keywords>%KEYWORDS%</keywords></action>
        </reference>


Make sure you clear your cache before checking the changes. Similarly, find the right XML file and whichever static page you want to make the changes. 




3 comments: