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
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.
Lukas says:
Great! Thanks for this post, saved us a lot of problems and time.
Laurent says:
Awesome! It works, but not for french special characters.
Any idea on this ?
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.
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.
Elio says:
Very nice work man!…I’ll use it for the short descriptions, getShortDescription() …it’s perfect. THANKS
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??
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()).'" />' ?>
Peter says:
Hi, If I want to use this to tag page, how to write.
PAJ says:
What do you mean by “to tag page”?
Peter says:
I mean this kind of page: http://www.magentodemostore.com/index.php/tag/product/list/tagId/23/
I want to change the meta description in that way.
Anokay says:
I have the same question. Maybe you have already a solution?
Ellys Directory says:
error at my website directory…do you have other clue??thanks
shawn says:
My markup tags aren’t being stripped, I am seeing p and strong tags in the description still. any tips. Thank you
PAJ says:
You might need to customize the code for your requirements depending on how your descriptions are formatted.
Mê Fornazari says:
Thank you a lot!
It saved me a lot of time 😀
Paul Mallam says:
Hi,
Excellent info given. Saved me a million hours worth of work…
🙂
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?
Blake says:
Is there a way to pull the first two sentences of the Description?
Thank you for the help.
Nigel says:
Wish I had found this sooner – many thanks, you saved me a LOT of time.
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?
PAJ says:
Yes you can, use the title and description info for the OG title and descriptions meta tags.
shaiikh imran says:
thanks you save my lots of time and solved my long time pending job,
thanks a lot
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 é, è, à …..
shraga kazatchkov says:
Really good!
thank you very much!
this code still works perfectly.