Editing meta tags for core pages in Magento

It’s easy enough to edit the default meta tags, or for CMS pages, product pages and category pages. But it’s not immediately apparent how to edit default meta tags in Magento, although from an SEO perspective it’s frequently useful to be able to do so.

Fortunately, there’s a nice easy way to do this, through the layout files.

Step By Step


First of all, figure out which layout file controls the page in question (this is normally pretty easy to do – anything in the customer area is controlled by the customer.xml layout file, anything in the checkout area is controlled by the checkout.xml layout file, and so on).

Find the layout file in your theme folder’s layout folder – if it’s not there, then you’re using the base layout, so be sure to copy that across from app/design/frontend/base/default/layout/ to your layout folder before making any changes, to ensure that those changes survive any future upgrades.

Then find the relevant bit of the layout file which deals with the page in question – again, this is pretty easy to do, as it’ll have start and end tags which correspond to the page, so the forgot password section in the customer layout, for example, will have the following start and end tags :

<customer_account_forgotpassword translate="label>
...
</customer_account_forgotpassword>

There may, or may not, be a reference in the relevant section dealing with the head section of the page. If there is, it’ll look something like this :

<reference name="head">
...
</reference>

To edit the page title, keywords meta tag and description meta tag, one simply needs to add into that section one or more of the following :

<action method="setTitle" translate="title" module="customer""><title>The title you want (will apply to all pages in the customer management section unless otherwise over-riden)</title></action>
<action method="setKeywords"><title>the, keywords, you, want, to, add, in</title></action>
<action method="setDescription"><title>The description you want to show</title></action>

And that’s it – flush the cache, restart Apache (if you’re using APC) and restart Varnish (if you’re using Varnish) and your changes should show up immediately.

3 comments

  • Darren Felton

    Useful method way to add meta tags, or anything else to the for that matter, without having to edit page/html/head.phtml:

    <![CDATA[

    ]]>

    Because page/html/head.phtml contains a getChildHtml() ?> with no parameter passed to it, it will render out any blocks which have been added to it. When adding something small such as the above meta tags I prefer to use just Magento’s Mage_Core_Block_Text class and set the text contents of it.

    • Darren Felton

      Sorry… the comments of this site either does not allow for decent pasting of source code into here, or I just don’t know how.

      The gist of my message was to add a block of the type Mage_Core_Block_Text, to the Mage_Page_Block_Html_Head class (reference name=”head”). Then within the XML definition for your new block use the setText() method to set the contents of the block. I’ll sometimes use this approach to add meta tags, script tags which load scripts from external sites, etc.

      • Giles (author)

        Darren,

        Thanks for your posts – an interesting suggestion. Not sure if the WP plugin we’re using works for comments as it does for posts, but we just put the code in “pre” tags and it’s converted to the output you see above.

        Kind regards,

        Giles

Leave a Reply

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

Want to talk to us about your project?