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.
<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />
<?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.