In this article, you will know about how to add default image alt text on all the product page using the plugin. We hope it is very informatics for you because the purpose of image alt text is to improve accessibility by describing what an image is showing to visitors. It also helps to improve SEO and search engine crawlers.
Step 1: Need to Create a Custom module in Magento 2.
Step 2: After creating a custom module you need to create a di.xml file in etc folder and now declare the Plugin in di.xml.
File Directory Path: app/code/Webiators/SeoImagesAltText/etc/frontend/di.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0"?> <!-- /** * * @category Webiators * @package Webiators_SeoImagesAltText * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com/ ). */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Block\Product\View\Gallery"> <plugin name="wt_seoimagealtforproductpage" type="Webiators\SeoImagesAltText\Plugin\Frontend\Magento\Catalog\Block\Product\View\Gallery\SeoImagesAltText" sortOrder="1" /> </type> </config> |
Step 3: Now you need to Create a plugin
File Directory Path:
app/code/Webiators/SeoImagesAltText/Plugin/Frontend/Magento/Catalog/Block/Product/View/Gallery/SeoImagesAltText.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace Webiators\SeoImagesAltText\Plugin\Frontend\Magento\Catalog\Block\Product\View\Gallery; class SeoImagesAltText { public function afterGetGalleryImages($block, $images) { if ($images instanceof \Magento\Framework\Data\Collection) { $product = $block->getProduct(); foreach ($images as $image) { //check if label is set if(!$image->getLabel()){ $image->setLabel($product->getName()); } } } return $images; } } |
Step 3: Run Command:
1 2 3 4 5 6 7 |
php -dmemory_limit=5G bin/magento setup:upgrade php -dmemory_limit=5G bin/magento setup:di:compile php -dmemory_limit=5G bin/magento setup:static-content:deploy -f php -dmemory_limit=5G bin/magento cache:flush php -dmemory_limit=5G bin/magento cache:clean php -dmemory_limit=5G bin/magento indexer:reindex |
Step 4: Now Go to the product page and see the result.
Thank You 🙂