Getting rid of tags in the meta description of product pages in Magento?

We use the WYSIWYG editor to describe products, and they have HTML tags. However, Magento uses everything as described in the metadata description in the product page title, which makes it ugly when people share the page on social networks because the description consists of raw HTML tags.

For example, on this page, the meta description is as follows:

<meta name="description" content="&lt;div class=&quot;short-description&quot;&gt; &lt;div class=&quot;std&quot;&gt; &lt;ul&gt; &lt;li&gt;Colored bridesmaid dress made in lace and taffeta&lt;/li&gt; &lt;li&gt;The top is made of ivory French corded lace, the skirt is made of colored taffeta&lt;/li&gt; &lt;li&gt;Straight front neckline, V Back&lt;/li&gt; &lt;li" /> 

My question is, how can I get rid of tags, so only text is used in the description? I don’t know which template to look at. Any help would be appreciated! Thanks!

+4
source share
2 answers

meta tags appear in the app/design/frontend/{interface}/{theme}/template/page/html/head.phtml template as follows:

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

I think you can replace htmlspecialchars with strip_tags . You will probably get the best value for these tags.
I do not understand how this happens to you. Products are separated by fields for entering meta descriptions and keywords that do not use the WYSIWYG editor. If you use some kind of automatic filling of these fields from the product description, it might be nice to break the tags before filling in the fields.
[EDIT]
You can try replacing tags with spaces instead of removing them:

 $description = preg_replace('#<[^>]+>#', ' ', $this->getDescription()); 

then you can remove double spaces

 $description = preg_replace('!\s+!', ' ', $description); 
+3
source

Copy this file

 /app/code/core/Mage/Catalog/Block/Product/View.php 

in this folder (create it before!)

 app/code/local/Mage/Catalog/Block/Product/View.php 

Now find this code (line 67 in Magento 1.9.1.0)

 $description = $product->getMetaDescription(); if ($description) { $headBlock->setDescription( ($description) ); } else { $headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255)); } 

and edit it as

 $description = $product->getMetaDescription(); if ($description) { $headBlock->setDescription( ($description) ); } else { $strippeddesc = html_entity_decode(strip_tags($product->getDescription())); $headBlock->setDescription(Mage::helper('core/string')->substr($strippeddesc, 0, 255)); } 

I added $ strippeddesc with product description content, cleaned and correctly decoded.

Now we have some great metadata on google; -)

+2
source

Source: https://habr.com/ru/post/1491625/


All Articles