Set meta tag Magento 2

This tutotrial is about how to set Meta tag in magento 2. Magento is a very useful ecommerce platform.Since every Magento project requires a certain level of modification. This may involve changing an existing functionality or adding a new one. In this tutorial we will learn about how to set Meta tag in magento 2.This includes Meta title,Meta Keyword and Meta description. And in this tutorial we will learn how to do it programmatically in Magento 2.

Lets come to our topic and see an example and code.Below are the two methods. Making changes in the block and controller will help to getthe desired results.
 

Block


$this->pageConfig->getTitle()->set(__('First Hello World Module'));
//setting the page title
$this->pageConfig->setDescription('test description'); // set meta description
$this->pageConfig->setKeywords('test keywords'); // set meta keyword

 

Controller

 

namespace QaisarSatti\HelloWorld\Controller\Index;

use Magento\Framework\View\Result\PageFactory;

class Index extends Action {

protected $_resultPage;

public function __construct(
PageFactory $resultPage) {
$this->_resultPage = $resultPage;
}
public function execute() {
$result= $this->_resultPage->create();
$result->getConfig()->getTitle()->set("Meta Title"); //setting the page
$result->getConfig()->setDescription("Description"); // set meta description
$result->getConfig()->setKeywords("Key Words"); // set meta keyword

return $result;
}

I hope this tutorial is easy to understand and serve its purpose.
For any suggestions & question, please feel free to drop a comment.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

5 thoughts on “Set meta tag Magento 2

  1. I do not even know how I ended up here, but I thought this post was great.

    I don’t know who you are but certainly you’re going to a famous blogger
    if you are not already 😉 Cheers!

Leave a Reply