Magento SEO – Generate META Description Tags Automatically

created January 10, 2011, last updated February 25, 2011.

.
closeThis post was last updated 13 years 2 months 12 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

Whilst some may argue that the META description tag has little SEO value, it is used by search engines to compile page “snippets” that are displayed next to the search results, and these snippets should contain meaningful and good SEO content. When you add a new product in Magento you need to think about your SEO keywords, page title and META tag content. If you have a lot of products and have not created META description tag content for them then it can be a lot of work to update all the products maually, and more work to keep them up to date. There are a couple of ways to make this easier, you can export all your products, and edit the META description for each product and then reimport them into Magento which is a very slow process, or you can automatically create the META description based on your products long description, your products should at least have good descriptions!

Product descriptions in our Magento shop usually contain the product name in bold type on the first line, followed by a few good SEO keyword rich product description sentences. We can use this product description to create a nice META description tag with just a few lines of code in the head.phtml template file of your theme.

For example, if we look at the sample database products, the Nokia 2610 product description looks like this

Sample database product description

The description META tag from the sample database for this product looks like this “Offering advanced media and calling features without breaking the bank, The Nokia 2610 is an easy to use

How about we use the product name and the first sentence from the description as the product description META tag for all our products? This can easily be accomplished by replacing the META description tag code in head.phtml located in your themes template/page/html/ folder.

Replace:

<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />

With:

<?php if (Mage::registry('current_product')) : ?>
<?php if (strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1)))=="") : ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1))).'" />' ?>
<?php endif; ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php endif; ?>

This code will first check that we are working with a product page, it then takes the long product description text, replaces the first html line break with a comma, extracts the text to the first full stop and then strips out any markup tags. If the resulting description is empty then we revert to using the normal product META description.

The resulting slightly better looking META description tag for our product now looks like this Nokia 2610 Phone, The Nokia 2610 is an easy to use device that combines multiple messaging options including email, instant messaging, and more.Google will use this as the description snippet in search results. I use this same text for my automated product marketing tweets and Facebook content too – more on that later.

All our products should now have nicely formatted META description tags.  Play around with the str_replace function to extract the text you want from your product descriptions,  make sure you have your main SEO keywords in both the first sentence of your product description and your HTML Title and you are now on your way to better SEO content for all your products, and good looking content for your search engine description snippets.

Comments

    • PAJ says:

      I know this works ok with German special characters, what problem are you seeing with French characters? You can try using the htmlentities php function to convert all special characters in the description string to html.

  1. Jobswala says:

    Thanks a lot. Hope to implement in my website soon although I too share the belief that it is redundant for the purpose of SEO.

  2. Chris says:

    For those of us who don’t use the product name as part of the description, is there a way to pull the current product’s name into the meta description, getTitle maybe??

  3. Sjörn says:

    If you want this for categories too, you can use the following snippet instead:


    <?php if (strip_tags(str_replace("",", ",substr(Mage::registry('current_category')->getDescription(), 0, strpos(Mage::registry('current_category')->getDescription(), '.')+1)))=="") : ?>
    <?php echo 'getDescription()).'" />' ?>

    <?php echo '<meta name="description" content="'.strip_tags(str_replace("",", ",substr(Mage::registry('current_category')->getDescription(), 0, strpos(Mage::registry('current_category')->getDescription(), '.')+1))).'" />' ?>

    <?php echo 'getDescription()).'" />' ?>

  4. mak says:

    This code is not including the page title in the meta description.
    I get this result

    Brinquedos do Japão | Chaveiro de Fairy Tail 01

    Shoudnt the title be in the description content?

  5. Mark says:

    Nice tutorial. I have been trying to figure out how to add Facebook’s Open Graph Likes functionality for Magento products. Part of the process involves creating specific OG meta properties for each product. Could I use this method to generate those meta properties or is there a better approach?

  6. Xavier says:

    Thank you for this code.. I know it’w already an old post but If someone can tell me how to adapt this code for the french characters ?.. like the é, è, à …..

This site uses Akismet to reduce spam. Learn how your comment data is processed.