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

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

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Getting rid of tags in the meta description of product pages in Magento?

Cleaning Up Meta Descriptions for Product Pages in Magento

When working with Magento, you might encounter situations where your product descriptions contain HTML tags that end up being displayed in the meta content description of your page headers. This can lead to an unappealing and poorly formatted display when shared on social media platforms.

Let me guide you through the process of cleaning up these descriptions so only the plain text is used in the meta description field.

Understanding the Problem

The issue arises because Magento is inserting the raw HTML content of your product descriptions directly into the meta description tag. This results in a very messy and unreadable string that includes all the formatting elements like <div>, <ul>, and <li> tags.

In the example provided, you can see how Magento has preserved the HTML structure of the short description field:

<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;/ul&gt;
</div>
</div>

The Solution: Filtering the Description

To resolve this issue, you need to filter out the HTML tags from your product descriptions before they are inserted into the meta description. This can be achieved by using a template file and a block class.

Firstly, locate the app/design/frontend/your_package/your_theme/template/catalog/product/view.phtml file in your Magento installation. This is where you’ll find the code responsible for rendering the product page.

In this file, look for the following section:

$_product->getShortDescription();

Here, replace it with the following code that uses a custom block to strip out the HTML tags:

<?php echo $this->getLayout()->createBlock('Magento\\Catalog\\Block\\Product\\Template')->setData('product', $_product)->toHtml(); ?>

This line of code creates an instance of Magento\Catalog\Block\Product\Template and sets it to use the product data you provided. The toHtml() call then renders the block, which contains a filter to remove HTML tags.

Updating the Block

To implement this filter, create a new file called Product.php in the following directory:

app/code/Your_Company/Your_Module/Block/Product

In this file, define your custom block class that extends Magento\Catalog\Block\Product\Template:

namespace Your_Company\Your_Module\Block;

use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Framework\View\Element\Template;

class Product extends AbstractProduct implements Template
{
    public function toHtml(): string
    {
        $description = parent::toHtml();
        return strip_tags($description);
    }
}

This block class overrides the toHtml() method of the base class. It uses the strip_tags() function from PHP to remove all HTML tags from the product description before returning it.

Conclusion

By following these steps, you can effectively clean up the meta descriptions for your Magento product pages. The solution involves creating a custom block that strips out the HTML tags using the built-in PHP functions provided by Magento.

Remember to replace Your_Company and Your_Module in the code examples with the actual name of your company and module, respectively.

For more detailed information on how to create custom blocks and modules in Magento, I recommend checking out our comprehensive guide at https://laravelcompany.com/magento-development/custom-blocks-in-magento/. This resource provides an in-depth explanation of the process with practical examples.

In summary, by carefully managing your product descriptions and utilizing Magento’s built-in functionality for creating custom blocks, you can ensure that your meta descriptions are clean, readable, and optimized for social media sharing.

Catalog SEO Errors

Our network