I have custom block in my custom module, which call my frontend custom phtml file.

let's say file path: app\code\Vendor\Module\Block\Index\Index.php

I need to figure out the way to set meta title, meta keywords and meta description using Block

I already set the page title by using following code in Block

protected function _prepareLayout()

        $this->pageConfig->getTitle()->set(__('My page title'));
        return parent::_prepareLayout();

Any help, experience and knowledge sharing would be appreciated

  • Why you want use only Block?
    – Keyur Shah
    May 10 '19 at 6:11
  • Because I already set the page title through block, so I think there might be the way to set meta tags using block as well. if not tell me other way in answer but it should not use any extension May 10 '19 at 6:13

To Get exact idea how to set page meta data via block refer vendor/magento/module-cms/Block/Page.php

In this magento have a function

protected function _prepareLayout() {
    // ...
    $this->pageConfig->getTitle()->set(__('Custom Page title'));
    $this->pageConfig->setKeywords(__('Custom Page keywords'));
    $this->pageConfig->setDescription(__('Custom Page description'));

    // ...
    return parent::_prepareLayout();

so in other word i can say like use $this->pageConfig->setKeywords and $this->pageConfig->setDescription respectively to set meta keyword and meta description.

Hope you will get your expected output.

  • let me try this, come back to you May 10 '19 at 6:20
  • @MuhammadHasham let us know the feedback, happy to help you. May 10 '19 at 6:22
  • Great It worked, Thanks :) I know it can be done through block and you got it right. May 10 '19 at 6:38

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.