How to change product page title programmatically in magento?

How to change product page title programmatically in magento?

September 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to change product page title programmatically in magento?

Changing Magento Product Page Titles Programmatically: A Comprehensive Guide

As an experienced Magento developer, I often get asked about ways to change product page titles dynamically based on specific attributes or category information. While the default title structure can be modified through the admin panel, it lacks the flexibility needed for more complex requirements. In this post, we’ll explore how to adjust product and category page titles programmatically using Magento 2.

Why Change Product Page Titles Programmatically?

Programmatic title changes offer several benefits:

  1. SEO Optimization: By including relevant keywords from your products or categories, you can boost search engine rankings.
  2. User Experience: A clear and concise title helps users understand the content of a page quickly.
  3. Flexibility: You can create dynamic titles that update based on changes in product attributes.

Changing Product Page Titles

To modify the title structure for individual products, you’ll need to work with the Magento\Catalog\Controller\Product class and adjust the _initProductActions() method.

First, extend the existing controller:

class Index extends \Magento\Catalog\Controller\Product\Index
{
    /**
     * @param ActionInterface $action
     * @return void
     */
    protected function _initProductActions(ActionInterface $action)
    {
        parent::_initProductActions($action);

        // Add your custom logic here

        return $this;
    }
}

Next, add your custom title generation code within the _initProductActions() method. For example:

public function execute()
{
    $product = $this->_initProductActions($this->getRequest()->getParam('id'));

    // Get the product attributes (author name and book name)
    $authorName = $product->getData('author_name');
    $bookName = $product->getData('book_name');

    // Generate the new title
    $newTitle = "{$authorName} by {$bookName}";

    // Set the new title for the page
    $this->_view->pageConfig->setTitle($newTitle);

    return $this->_view->getLayout()->createBlock('Magento\Framework\View\Result\Page');
}

In this example, we assume that your product has ‘author_name’ and ‘book_name’ attributes. The generated title will be displayed as the page title.

Changing Category Page Titles

To modify category page titles, work with the Magento\Catalog\Controller\Category class instead:

class Index extends \Magento\Catalog\Controller\Category\Index
{
    public function execute()
    {
        // Get the current category
        $category = $this->_initCategoryActions();

        // Get the category name and generate a new title (e.g., "Books - {$categoryName}")
        $newTitle = "Books - {$category->getName()}";

        // Set the new title for the page
        $this->_view->pageConfig->setTitle($newTitle);

        return $this->_view->getLayout()->createBlock('Magento\Framework\View\Result\Page');
    }
}

Conclusion

Changing Magento product and category page titles programmatically offers a powerful way to optimize your store’s SEO, enhance user experience, and maintain consistent branding.

Remember that this approach requires a solid understanding of the Magento 2 architecture. If you’re new to the platform or unsure about any part of this process, don’t hesitate to reach out to our team at Laravel Company.

We specialize in helping businesses like yours unlock the full potential of Magento with custom development and optimization services tailored to your unique requirements. Contact us today to discuss how we can assist you in implementing these changes or addressing any other Magento questions you may have.

Stay tuned to our blog for more Magento tips, tricks, and best practices!

Magento 2 Catalog Indexing Performance

Our network