Dynamically add year variable to meta title / description in Magento 2

Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?

If there's something that implicates the president why is there then a national security issue? (John Dowd)

Electricity free spaceship

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

tabular: caption and align problem

Was planting UN flag on Moon ever discussed?

Can you make an identity from this product?

Does a bank have to tell me if a check made out to me was cashed there?

Why does this query, missing a FROM clause, not error out?

How can one's career as a reviewer be ended?

What STL algorithm can determine if exactly one item in a container satisfies a predicate?

Do people with slow metabolism tend to gain weight (fat) if they stop exercising?

Java Servlet & JSP simple login

Why is long-term living in Almost-Earth causing severe health problems?

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

Does the Nuka-Cola bottler actually generate nuka cola?

Ability To Change Root User Password (Vulnerability?)

What aircraft was used as Air Force One for the flight between Southampton and Shannon?

Why are MBA programs closing in the United States?

Increase speed altering column on large table to NON NULL

Proving that a Russian cryptographic standard is too structured

2019 gold coins to share

Analogy between an unknown in an argument, and a contradiction in the principle of explosion

Live action TV show where High school Kids go into the virtual world and have to clear levels



Dynamically add year variable to meta title / description in Magento 2







.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















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?










share|improve this question






















  • for a particular category or all categories?

    – Amit Bera
    Jun 3 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 at 13:11

















1















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?










share|improve this question






















  • for a particular category or all categories?

    – Amit Bera
    Jun 3 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 at 13:11













1












1








1








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?










share|improve this question














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?







magento2 variables meta-title meta-description






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 3 at 12:57









klaaskoxklaaskox

104




104












  • for a particular category or all categories?

    – Amit Bera
    Jun 3 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 at 13:11

















  • for a particular category or all categories?

    – Amit Bera
    Jun 3 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 at 13:11
















for a particular category or all categories?

– Amit Bera
Jun 3 at 12:59





for a particular category or all categories?

– Amit Bera
Jun 3 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 at 13:11





For all categories in which the year tag is used. I just want to replace this tag with the current year.

– klaaskox
Jun 3 at 13:11










1 Answer
1






active

oldest

votes


















0














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="StackExchangeMagentoObserverFrontendCatalogCategoryLoadAfter"
name="stackexchange_magento_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
/>
</event>
</config>



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




CategoryLoadAfter.php code



<?php
namespace StackExchangeMagentoObserverFrontendCatalog;

class CategoryLoadAfter implements MagentoFrameworkEventObserverInterface

/**
* @var PsrLogLoggerInterface
*/
private $logger;

public function __construct(
PsrLogLoggerInterface $logger
)

$this->logger = $logger;

/**
* Execute observer
*
* @param MagentoFrameworkEventObserver $observer
* @return void
*/
public function execute(
MagentoFrameworkEventObserver $observer
)
$this->logger->debug(__METHOD__);
$category = $observer->getEvent()->getCategory();
if ($category instanceof MagentoCatalogModelCategory)
$oldMetaTitle = $category->getMetaTitle();
$oldMetaDescription = $category->getMetaDescription();
$oldMetaKeyword = $category->getMetaKeywords();
$category->setMetaTitle(str_replace("year",date('Y'),$oldMetaTitle));
$category->setMetaDescription(str_replace("year",date('Y'),$oldMetaDescription));
$category->setMetaKeywords(str_replace("year",date('Y'),$oldMetaKeyword));






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.






share|improve this answer

























  • Thanks a lot, works great!

    – klaaskox
    Jun 3 at 14:54











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f277087%2fdynamically-add-year-variable-to-meta-title-description-in-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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="StackExchangeMagentoObserverFrontendCatalogCategoryLoadAfter"
name="stackexchange_magento_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
/>
</event>
</config>



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




CategoryLoadAfter.php code



<?php
namespace StackExchangeMagentoObserverFrontendCatalog;

class CategoryLoadAfter implements MagentoFrameworkEventObserverInterface

/**
* @var PsrLogLoggerInterface
*/
private $logger;

public function __construct(
PsrLogLoggerInterface $logger
)

$this->logger = $logger;

/**
* Execute observer
*
* @param MagentoFrameworkEventObserver $observer
* @return void
*/
public function execute(
MagentoFrameworkEventObserver $observer
)
$this->logger->debug(__METHOD__);
$category = $observer->getEvent()->getCategory();
if ($category instanceof MagentoCatalogModelCategory)
$oldMetaTitle = $category->getMetaTitle();
$oldMetaDescription = $category->getMetaDescription();
$oldMetaKeyword = $category->getMetaKeywords();
$category->setMetaTitle(str_replace("year",date('Y'),$oldMetaTitle));
$category->setMetaDescription(str_replace("year",date('Y'),$oldMetaDescription));
$category->setMetaKeywords(str_replace("year",date('Y'),$oldMetaKeyword));






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.






share|improve this answer

























  • Thanks a lot, works great!

    – klaaskox
    Jun 3 at 14:54















0














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="StackExchangeMagentoObserverFrontendCatalogCategoryLoadAfter"
name="stackexchange_magento_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
/>
</event>
</config>



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




CategoryLoadAfter.php code



<?php
namespace StackExchangeMagentoObserverFrontendCatalog;

class CategoryLoadAfter implements MagentoFrameworkEventObserverInterface

/**
* @var PsrLogLoggerInterface
*/
private $logger;

public function __construct(
PsrLogLoggerInterface $logger
)

$this->logger = $logger;

/**
* Execute observer
*
* @param MagentoFrameworkEventObserver $observer
* @return void
*/
public function execute(
MagentoFrameworkEventObserver $observer
)
$this->logger->debug(__METHOD__);
$category = $observer->getEvent()->getCategory();
if ($category instanceof MagentoCatalogModelCategory)
$oldMetaTitle = $category->getMetaTitle();
$oldMetaDescription = $category->getMetaDescription();
$oldMetaKeyword = $category->getMetaKeywords();
$category->setMetaTitle(str_replace("year",date('Y'),$oldMetaTitle));
$category->setMetaDescription(str_replace("year",date('Y'),$oldMetaDescription));
$category->setMetaKeywords(str_replace("year",date('Y'),$oldMetaKeyword));






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.






share|improve this answer

























  • Thanks a lot, works great!

    – klaaskox
    Jun 3 at 14:54













0












0








0







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="StackExchangeMagentoObserverFrontendCatalogCategoryLoadAfter"
name="stackexchange_magento_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
/>
</event>
</config>



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




CategoryLoadAfter.php code



<?php
namespace StackExchangeMagentoObserverFrontendCatalog;

class CategoryLoadAfter implements MagentoFrameworkEventObserverInterface

/**
* @var PsrLogLoggerInterface
*/
private $logger;

public function __construct(
PsrLogLoggerInterface $logger
)

$this->logger = $logger;

/**
* Execute observer
*
* @param MagentoFrameworkEventObserver $observer
* @return void
*/
public function execute(
MagentoFrameworkEventObserver $observer
)
$this->logger->debug(__METHOD__);
$category = $observer->getEvent()->getCategory();
if ($category instanceof MagentoCatalogModelCategory)
$oldMetaTitle = $category->getMetaTitle();
$oldMetaDescription = $category->getMetaDescription();
$oldMetaKeyword = $category->getMetaKeywords();
$category->setMetaTitle(str_replace("year",date('Y'),$oldMetaTitle));
$category->setMetaDescription(str_replace("year",date('Y'),$oldMetaDescription));
$category->setMetaKeywords(str_replace("year",date('Y'),$oldMetaKeyword));






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.






share|improve this answer















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="StackExchangeMagentoObserverFrontendCatalogCategoryLoadAfter"
name="stackexchange_magento_observer_frontend_catalog_categoryloadafter_catalog_product_load_after"
/>
</event>
</config>



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




CategoryLoadAfter.php code



<?php
namespace StackExchangeMagentoObserverFrontendCatalog;

class CategoryLoadAfter implements MagentoFrameworkEventObserverInterface

/**
* @var PsrLogLoggerInterface
*/
private $logger;

public function __construct(
PsrLogLoggerInterface $logger
)

$this->logger = $logger;

/**
* Execute observer
*
* @param MagentoFrameworkEventObserver $observer
* @return void
*/
public function execute(
MagentoFrameworkEventObserver $observer
)
$this->logger->debug(__METHOD__);
$category = $observer->getEvent()->getCategory();
if ($category instanceof MagentoCatalogModelCategory)
$oldMetaTitle = $category->getMetaTitle();
$oldMetaDescription = $category->getMetaDescription();
$oldMetaKeyword = $category->getMetaKeywords();
$category->setMetaTitle(str_replace("year",date('Y'),$oldMetaTitle));
$category->setMetaDescription(str_replace("year",date('Y'),$oldMetaDescription));
$category->setMetaKeywords(str_replace("year",date('Y'),$oldMetaKeyword));






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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 3 at 14:57









klaaskox

104




104










answered Jun 3 at 13:05









Amit BeraAmit Bera

61k1682181




61k1682181












  • Thanks a lot, works great!

    – klaaskox
    Jun 3 at 14:54

















  • Thanks a lot, works great!

    – klaaskox
    Jun 3 at 14:54
















Thanks a lot, works great!

– klaaskox
Jun 3 at 14:54





Thanks a lot, works great!

– klaaskox
Jun 3 at 14:54

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f277087%2fdynamically-add-year-variable-to-meta-title-description-in-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form