I am trying to automatically add the current year to the meta title and description in for example category-pages. I want to achieve something like this:

Meta title: "The best category items for {{year}}" >> "The best category items for 2019"

What would be the easiest way to do this?

  • for a particular category or all categories? – Amit Bera Jun 3 '19 at 12:59
  • For all categories in which the {{year}} tag is used. I just want to replace this tag with the current year. – klaaskox Jun 3 '19 at 13:11

In this case, you can use event/observer.

Fire an observer on the event catalog_category_load_after at frontend area set meta description on the fly.

Create events.xml at app/code/StackExchange/Magento/etc/frontend/ and as `events.xml location under on frontend and this event only fire for frontend area.

events.xml code

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_category_load_after">
        <observer instance="StackExchange\Magento\Observer\Frontend\Catalog\CategoryLoadAfter" 

Create Observer class CategoryLoadAfter at app/code/StackExchange/Magento/Observer/Frontend/Catalog/.

CategoryLoadAfter.php code

namespace StackExchange\Magento\Observer\Frontend\Catalog;

class CategoryLoadAfter implements \Magento\Framework\Event\ObserverInterface {

     * @var \Psr\Log\LoggerInterface
    private $logger;

    public function __construct(
     \Psr\Log\LoggerInterface $logger
    ) {

        $this->logger = $logger;
     * Execute observer
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $category = $observer->getEvent()->getCategory();
        if ($category instanceof \Magento\Catalog\Model\Category) {
            $oldMetaTitle = $category->getMetaTitle();
            $oldMetaDescription = $category->getMetaDescription();
            $oldMetaKeyword = $category->getMetaKeywords();


Also, your module must have:

  1. app/code/{Vendor}/{Modulename}/etc/module.xml
  2. app/code/{Vendor}/{Modulename}/composer.json
  3. app/code/{Vendor}/{Modulename}/registration.php

After adding the event you should flush the cache.

| improve this answer | |
  • Thanks a lot, works great! – klaaskox Jun 3 '19 at 14:54
  • How could set the month and year, for example {{month}} {{year}} – user2240778 May 27 at 11:19
  • I figured it out date('F Y'), but what if I want to use the month and year separately for some meta titles. For example "offers for {{month}}" or "best sales of {{year}}", or combination "best offer for {{month}} {{year}}" – user2240778 May 27 at 11:31
  • yes, you can what you want. Just change require a value at respective method like setMetaTitle – Amit Bera May 27 at 13:21
  • sorry, but how do I do that? – user2240778 May 27 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.