Truncate Meta Title and Meta Description in Magento2SEO: Product Meta Title, Keywords and DescriptionSetting different title and meta title for a pageHow can i Bulk Change Category Page Title, Meta Description/KeywordsDefault meta title for product and category pageHow can i check all my categories have META Tags Values (Title, Desc, Keywords)Updated meta title and description for site pages but changes are not reflected on page browser source.'Migration Meta Information' is not updating when I edit product title/name in magento 2.3Dynamically add year variable to meta title / description in Magento 2Magento2 : Not showing Meta title & description on category pageHow to Remove meta title tag in magento 2

Is it possible to eat quietly in Minecraft?

How do changes to your speed that occur on your own turn affect your available movement?

Idioms: Should it be " the internet is a seemingly infinite well of information" or "the internet is a seemingly infinite wealth of information"

Why does the salt in the oceans not sink to the bottom?

Why did NASA use Imperial units?

How to write a sincerely religious protagonist without preaching or affirming or judging their worldview?

Is it possible to build or embed the SMILES representation of compounds in 3D?

The seven story archetypes. Are they truly all of them?

Navigating the multiverse of bifurcated parallel realities

What is "ass door"?

My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?

Aging LEDs - does their output drop after turn-on?

What's the 1 inch size square knob sticking out of wall?

How can Kazakhstan perform MITM attacks on all HTTPS traffic?

How important is a good quality camera for good photography?

I have a domain, static IP address and many devices I'd like to access outside my house. How do I route them?

Why did modems have speakers?

Character Frequency in a String

The 50,000 row query limit is not actually a "per APEX call" as widely believed

Company requiring me to let them review research from before I was hired

How can the artificial womb be made affordable for the common people?

Using paddles to support a bug net

Why is a dedicated QA team member necessary?

Can I pay with HKD in Macau or Shenzhen?



Truncate Meta Title and Meta Description in Magento2


SEO: Product Meta Title, Keywords and DescriptionSetting different title and meta title for a pageHow can i Bulk Change Category Page Title, Meta Description/KeywordsDefault meta title for product and category pageHow can i check all my categories have META Tags Values (Title, Desc, Keywords)Updated meta title and description for site pages but changes are not reflected on page browser source.'Migration Meta Information' is not updating when I edit product title/name in magento 2.3Dynamically add year variable to meta title / description in Magento 2Magento2 : Not showing Meta title & description on category pageHow to Remove meta title tag in magento 2






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








1















turncate Meta title and meta description on pdp page how we can do that in which file we need to make changes .










share|improve this question
























  • what you mean by truncate?

    – Amit Bera
    Jul 15 at 6:44











  • i want turncate length

    – werwer
    Jul 15 at 6:48











  • Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “Product Title” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages.

    – werwer
    Jul 15 at 6:48











  • i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ?

    – Anas Mansuri
    Jul 15 at 7:28












  • for seo on ogogle

    – werwer
    Jul 15 at 7:32

















1















turncate Meta title and meta description on pdp page how we can do that in which file we need to make changes .










share|improve this question
























  • what you mean by truncate?

    – Amit Bera
    Jul 15 at 6:44











  • i want turncate length

    – werwer
    Jul 15 at 6:48











  • Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “Product Title” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages.

    – werwer
    Jul 15 at 6:48











  • i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ?

    – Anas Mansuri
    Jul 15 at 7:28












  • for seo on ogogle

    – werwer
    Jul 15 at 7:32













1












1








1








turncate Meta title and meta description on pdp page how we can do that in which file we need to make changes .










share|improve this question
















turncate Meta title and meta description on pdp page how we can do that in which file we need to make changes .







magento2 seo meta-title






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 15 at 8:56









Mohit Rane

87217 bronze badges




87217 bronze badges










asked Jul 15 at 6:41









werwerwerwer

997 bronze badges




997 bronze badges












  • what you mean by truncate?

    – Amit Bera
    Jul 15 at 6:44











  • i want turncate length

    – werwer
    Jul 15 at 6:48











  • Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “Product Title” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages.

    – werwer
    Jul 15 at 6:48











  • i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ?

    – Anas Mansuri
    Jul 15 at 7:28












  • for seo on ogogle

    – werwer
    Jul 15 at 7:32

















  • what you mean by truncate?

    – Amit Bera
    Jul 15 at 6:44











  • i want turncate length

    – werwer
    Jul 15 at 6:48











  • Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “Product Title” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages.

    – werwer
    Jul 15 at 6:48











  • i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ?

    – Anas Mansuri
    Jul 15 at 7:28












  • for seo on ogogle

    – werwer
    Jul 15 at 7:32
















what you mean by truncate?

– Amit Bera
Jul 15 at 6:44





what you mean by truncate?

– Amit Bera
Jul 15 at 6:44













i want turncate length

– werwer
Jul 15 at 6:48





i want turncate length

– werwer
Jul 15 at 6:48













Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “Product Title” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages.

– werwer
Jul 15 at 6:48





Meta Title Case 1: When the length of the Product Title is exceeding 60 Characters including the white spaces. Then Truncate it up to 60 Characters and use as “Meta Title” Proposed Meta Title : “Product Title” (But Truncated up to 60 Characters Only) Truncate the length of Product Title up to 60 Characters and use them for Meta Title of Product level pages.

– werwer
Jul 15 at 6:48













i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ?

– Anas Mansuri
Jul 15 at 7:28






i suggest to create custom script and than replace title and description. first get title and count word length and then truncate . one more thing why only in pdp page ?

– Anas Mansuri
Jul 15 at 7:28














for seo on ogogle

– werwer
Jul 15 at 7:32





for seo on ogogle

– werwer
Jul 15 at 7:32










1 Answer
1






active

oldest

votes


















0














In PDP, meta title and description is set via the product view helper class: MagentoCatalogHelperProductView::preparePageMetadata().



So what you need to do is write a preference for this helper class as the method preparePageMetadata() is a private method. But, the best option would be doing the same through a plugin. To do this, you need to write an around plugin for the method MagentoCatalogHelperProductView:: prepareAndRender() which is what actually call the private method preparePageMetadata().



So I will just show you how you can achieve this through a plugin.



File: app/code/[Namespace]/[Module]/etc/frontend/di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogHelperProductView">
<plugin
name="NamespaceModuleAlterPDPMetaTags"
type="NamespaceModulePluginCatalogHelperProductViewPlugin" />
</type>
</config>


Basically, this file will register our plugin dependency inside our module Namespace_Module.



File: app/code/[Namespace]/[Module]/Plugin/Catalog/Helper/Product/ViewPlugin.php



<?php

namespace NamespaceModulePluginCatalogHelperProduct;

use MagentoCatalogHelperProductView as ProductViewHelper;
use MagentoFrameworkRegistry;
use MagentoFrameworkViewResultPage;
use MagentoFrameworkStdlibStringUtils;


class ViewPlugin

/**
* @var MagentoFrameworkRegistry $coreRegistry,
*/
private $coreRegistry;

private $resultPage;

private $string;

public function __construct(Registry $coreRegistry, StringUtils $string)

$this->coreRegistry = $coreRegistry;
$this->string = $string;


public function aroundPrepareAndRender(
ProductViewHelper $productViewHelper,
callable $proceed,
Page $resultPage,
$productId,
$controller,
$params = null
)
// collecting result page instance for future use
$this->resultPage = $resultPage;

// send the control to the original method
$result = $proceed($resultPage, $productId, $controller, $params);

// after meta title and description is generated, this will be executed

// make sure the helper instance is correct
if (!$result instanceof ProductViewHelper)
return $productViewHelper;


// collecting product information from the core registry
$product = $this->coreRegistry->registry('product');

// changing meta title and description as needed
$this->alterMetaTitle($product);
$this->alterMetaDescription($product);

return $result;


/**
* Changing meta title of PDP; Allowing only 60 characters
* @param $product
*/
private function alterMetaTitle($product)

$pageConfig = $this->resultPage->getConfig();
$title = $product->getMetaTitle();

if ($title)
$pageConfig->getTitle()->set($this->string->substr($title, 0, 60));



/**
* Changing meta description of PDP; Allowing only 60 characters
* @param $product
*/
private function alterMetaDescription($product)

$pageConfig = $this->resultPage->getConfig();
$description = $product->getMetaDescription();

if ($description)
$pageConfig->setDescription($this->string->substr($description, 0, 60));
else
$pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 60));





As you can see, we wrote an around plugin for the method prepareAndRender(). Why we are relying on an around plugin is because we need the parameter $resultPage of the method in order to alter the meta title and description. Also, we need $product instance in order to retrieve the meta title and description. Product initialization is also happening inside prepareAndRender() method and hence will be obtained through the registry.



You can customize the methods alterMetaTitle() and alterMetaDescription() as per your need. Currently, it will enforce 60 character limit to both meta title and description.



I hope that gives you an idea.



Note: Module creation and usage of the plugin is out of scope concepts as per this question. If you need to know more details, please refer devdocs.






share|improve this answer

























    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%2f282052%2ftruncate-meta-title-and-meta-description-in-magento2%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 PDP, meta title and description is set via the product view helper class: MagentoCatalogHelperProductView::preparePageMetadata().



    So what you need to do is write a preference for this helper class as the method preparePageMetadata() is a private method. But, the best option would be doing the same through a plugin. To do this, you need to write an around plugin for the method MagentoCatalogHelperProductView:: prepareAndRender() which is what actually call the private method preparePageMetadata().



    So I will just show you how you can achieve this through a plugin.



    File: app/code/[Namespace]/[Module]/etc/frontend/di.xml



    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoCatalogHelperProductView">
    <plugin
    name="NamespaceModuleAlterPDPMetaTags"
    type="NamespaceModulePluginCatalogHelperProductViewPlugin" />
    </type>
    </config>


    Basically, this file will register our plugin dependency inside our module Namespace_Module.



    File: app/code/[Namespace]/[Module]/Plugin/Catalog/Helper/Product/ViewPlugin.php



    <?php

    namespace NamespaceModulePluginCatalogHelperProduct;

    use MagentoCatalogHelperProductView as ProductViewHelper;
    use MagentoFrameworkRegistry;
    use MagentoFrameworkViewResultPage;
    use MagentoFrameworkStdlibStringUtils;


    class ViewPlugin

    /**
    * @var MagentoFrameworkRegistry $coreRegistry,
    */
    private $coreRegistry;

    private $resultPage;

    private $string;

    public function __construct(Registry $coreRegistry, StringUtils $string)

    $this->coreRegistry = $coreRegistry;
    $this->string = $string;


    public function aroundPrepareAndRender(
    ProductViewHelper $productViewHelper,
    callable $proceed,
    Page $resultPage,
    $productId,
    $controller,
    $params = null
    )
    // collecting result page instance for future use
    $this->resultPage = $resultPage;

    // send the control to the original method
    $result = $proceed($resultPage, $productId, $controller, $params);

    // after meta title and description is generated, this will be executed

    // make sure the helper instance is correct
    if (!$result instanceof ProductViewHelper)
    return $productViewHelper;


    // collecting product information from the core registry
    $product = $this->coreRegistry->registry('product');

    // changing meta title and description as needed
    $this->alterMetaTitle($product);
    $this->alterMetaDescription($product);

    return $result;


    /**
    * Changing meta title of PDP; Allowing only 60 characters
    * @param $product
    */
    private function alterMetaTitle($product)

    $pageConfig = $this->resultPage->getConfig();
    $title = $product->getMetaTitle();

    if ($title)
    $pageConfig->getTitle()->set($this->string->substr($title, 0, 60));



    /**
    * Changing meta description of PDP; Allowing only 60 characters
    * @param $product
    */
    private function alterMetaDescription($product)

    $pageConfig = $this->resultPage->getConfig();
    $description = $product->getMetaDescription();

    if ($description)
    $pageConfig->setDescription($this->string->substr($description, 0, 60));
    else
    $pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 60));





    As you can see, we wrote an around plugin for the method prepareAndRender(). Why we are relying on an around plugin is because we need the parameter $resultPage of the method in order to alter the meta title and description. Also, we need $product instance in order to retrieve the meta title and description. Product initialization is also happening inside prepareAndRender() method and hence will be obtained through the registry.



    You can customize the methods alterMetaTitle() and alterMetaDescription() as per your need. Currently, it will enforce 60 character limit to both meta title and description.



    I hope that gives you an idea.



    Note: Module creation and usage of the plugin is out of scope concepts as per this question. If you need to know more details, please refer devdocs.






    share|improve this answer



























      0














      In PDP, meta title and description is set via the product view helper class: MagentoCatalogHelperProductView::preparePageMetadata().



      So what you need to do is write a preference for this helper class as the method preparePageMetadata() is a private method. But, the best option would be doing the same through a plugin. To do this, you need to write an around plugin for the method MagentoCatalogHelperProductView:: prepareAndRender() which is what actually call the private method preparePageMetadata().



      So I will just show you how you can achieve this through a plugin.



      File: app/code/[Namespace]/[Module]/etc/frontend/di.xml



      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <type name="MagentoCatalogHelperProductView">
      <plugin
      name="NamespaceModuleAlterPDPMetaTags"
      type="NamespaceModulePluginCatalogHelperProductViewPlugin" />
      </type>
      </config>


      Basically, this file will register our plugin dependency inside our module Namespace_Module.



      File: app/code/[Namespace]/[Module]/Plugin/Catalog/Helper/Product/ViewPlugin.php



      <?php

      namespace NamespaceModulePluginCatalogHelperProduct;

      use MagentoCatalogHelperProductView as ProductViewHelper;
      use MagentoFrameworkRegistry;
      use MagentoFrameworkViewResultPage;
      use MagentoFrameworkStdlibStringUtils;


      class ViewPlugin

      /**
      * @var MagentoFrameworkRegistry $coreRegistry,
      */
      private $coreRegistry;

      private $resultPage;

      private $string;

      public function __construct(Registry $coreRegistry, StringUtils $string)

      $this->coreRegistry = $coreRegistry;
      $this->string = $string;


      public function aroundPrepareAndRender(
      ProductViewHelper $productViewHelper,
      callable $proceed,
      Page $resultPage,
      $productId,
      $controller,
      $params = null
      )
      // collecting result page instance for future use
      $this->resultPage = $resultPage;

      // send the control to the original method
      $result = $proceed($resultPage, $productId, $controller, $params);

      // after meta title and description is generated, this will be executed

      // make sure the helper instance is correct
      if (!$result instanceof ProductViewHelper)
      return $productViewHelper;


      // collecting product information from the core registry
      $product = $this->coreRegistry->registry('product');

      // changing meta title and description as needed
      $this->alterMetaTitle($product);
      $this->alterMetaDescription($product);

      return $result;


      /**
      * Changing meta title of PDP; Allowing only 60 characters
      * @param $product
      */
      private function alterMetaTitle($product)

      $pageConfig = $this->resultPage->getConfig();
      $title = $product->getMetaTitle();

      if ($title)
      $pageConfig->getTitle()->set($this->string->substr($title, 0, 60));



      /**
      * Changing meta description of PDP; Allowing only 60 characters
      * @param $product
      */
      private function alterMetaDescription($product)

      $pageConfig = $this->resultPage->getConfig();
      $description = $product->getMetaDescription();

      if ($description)
      $pageConfig->setDescription($this->string->substr($description, 0, 60));
      else
      $pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 60));





      As you can see, we wrote an around plugin for the method prepareAndRender(). Why we are relying on an around plugin is because we need the parameter $resultPage of the method in order to alter the meta title and description. Also, we need $product instance in order to retrieve the meta title and description. Product initialization is also happening inside prepareAndRender() method and hence will be obtained through the registry.



      You can customize the methods alterMetaTitle() and alterMetaDescription() as per your need. Currently, it will enforce 60 character limit to both meta title and description.



      I hope that gives you an idea.



      Note: Module creation and usage of the plugin is out of scope concepts as per this question. If you need to know more details, please refer devdocs.






      share|improve this answer

























        0












        0








        0







        In PDP, meta title and description is set via the product view helper class: MagentoCatalogHelperProductView::preparePageMetadata().



        So what you need to do is write a preference for this helper class as the method preparePageMetadata() is a private method. But, the best option would be doing the same through a plugin. To do this, you need to write an around plugin for the method MagentoCatalogHelperProductView:: prepareAndRender() which is what actually call the private method preparePageMetadata().



        So I will just show you how you can achieve this through a plugin.



        File: app/code/[Namespace]/[Module]/etc/frontend/di.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCatalogHelperProductView">
        <plugin
        name="NamespaceModuleAlterPDPMetaTags"
        type="NamespaceModulePluginCatalogHelperProductViewPlugin" />
        </type>
        </config>


        Basically, this file will register our plugin dependency inside our module Namespace_Module.



        File: app/code/[Namespace]/[Module]/Plugin/Catalog/Helper/Product/ViewPlugin.php



        <?php

        namespace NamespaceModulePluginCatalogHelperProduct;

        use MagentoCatalogHelperProductView as ProductViewHelper;
        use MagentoFrameworkRegistry;
        use MagentoFrameworkViewResultPage;
        use MagentoFrameworkStdlibStringUtils;


        class ViewPlugin

        /**
        * @var MagentoFrameworkRegistry $coreRegistry,
        */
        private $coreRegistry;

        private $resultPage;

        private $string;

        public function __construct(Registry $coreRegistry, StringUtils $string)

        $this->coreRegistry = $coreRegistry;
        $this->string = $string;


        public function aroundPrepareAndRender(
        ProductViewHelper $productViewHelper,
        callable $proceed,
        Page $resultPage,
        $productId,
        $controller,
        $params = null
        )
        // collecting result page instance for future use
        $this->resultPage = $resultPage;

        // send the control to the original method
        $result = $proceed($resultPage, $productId, $controller, $params);

        // after meta title and description is generated, this will be executed

        // make sure the helper instance is correct
        if (!$result instanceof ProductViewHelper)
        return $productViewHelper;


        // collecting product information from the core registry
        $product = $this->coreRegistry->registry('product');

        // changing meta title and description as needed
        $this->alterMetaTitle($product);
        $this->alterMetaDescription($product);

        return $result;


        /**
        * Changing meta title of PDP; Allowing only 60 characters
        * @param $product
        */
        private function alterMetaTitle($product)

        $pageConfig = $this->resultPage->getConfig();
        $title = $product->getMetaTitle();

        if ($title)
        $pageConfig->getTitle()->set($this->string->substr($title, 0, 60));



        /**
        * Changing meta description of PDP; Allowing only 60 characters
        * @param $product
        */
        private function alterMetaDescription($product)

        $pageConfig = $this->resultPage->getConfig();
        $description = $product->getMetaDescription();

        if ($description)
        $pageConfig->setDescription($this->string->substr($description, 0, 60));
        else
        $pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 60));





        As you can see, we wrote an around plugin for the method prepareAndRender(). Why we are relying on an around plugin is because we need the parameter $resultPage of the method in order to alter the meta title and description. Also, we need $product instance in order to retrieve the meta title and description. Product initialization is also happening inside prepareAndRender() method and hence will be obtained through the registry.



        You can customize the methods alterMetaTitle() and alterMetaDescription() as per your need. Currently, it will enforce 60 character limit to both meta title and description.



        I hope that gives you an idea.



        Note: Module creation and usage of the plugin is out of scope concepts as per this question. If you need to know more details, please refer devdocs.






        share|improve this answer













        In PDP, meta title and description is set via the product view helper class: MagentoCatalogHelperProductView::preparePageMetadata().



        So what you need to do is write a preference for this helper class as the method preparePageMetadata() is a private method. But, the best option would be doing the same through a plugin. To do this, you need to write an around plugin for the method MagentoCatalogHelperProductView:: prepareAndRender() which is what actually call the private method preparePageMetadata().



        So I will just show you how you can achieve this through a plugin.



        File: app/code/[Namespace]/[Module]/etc/frontend/di.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCatalogHelperProductView">
        <plugin
        name="NamespaceModuleAlterPDPMetaTags"
        type="NamespaceModulePluginCatalogHelperProductViewPlugin" />
        </type>
        </config>


        Basically, this file will register our plugin dependency inside our module Namespace_Module.



        File: app/code/[Namespace]/[Module]/Plugin/Catalog/Helper/Product/ViewPlugin.php



        <?php

        namespace NamespaceModulePluginCatalogHelperProduct;

        use MagentoCatalogHelperProductView as ProductViewHelper;
        use MagentoFrameworkRegistry;
        use MagentoFrameworkViewResultPage;
        use MagentoFrameworkStdlibStringUtils;


        class ViewPlugin

        /**
        * @var MagentoFrameworkRegistry $coreRegistry,
        */
        private $coreRegistry;

        private $resultPage;

        private $string;

        public function __construct(Registry $coreRegistry, StringUtils $string)

        $this->coreRegistry = $coreRegistry;
        $this->string = $string;


        public function aroundPrepareAndRender(
        ProductViewHelper $productViewHelper,
        callable $proceed,
        Page $resultPage,
        $productId,
        $controller,
        $params = null
        )
        // collecting result page instance for future use
        $this->resultPage = $resultPage;

        // send the control to the original method
        $result = $proceed($resultPage, $productId, $controller, $params);

        // after meta title and description is generated, this will be executed

        // make sure the helper instance is correct
        if (!$result instanceof ProductViewHelper)
        return $productViewHelper;


        // collecting product information from the core registry
        $product = $this->coreRegistry->registry('product');

        // changing meta title and description as needed
        $this->alterMetaTitle($product);
        $this->alterMetaDescription($product);

        return $result;


        /**
        * Changing meta title of PDP; Allowing only 60 characters
        * @param $product
        */
        private function alterMetaTitle($product)

        $pageConfig = $this->resultPage->getConfig();
        $title = $product->getMetaTitle();

        if ($title)
        $pageConfig->getTitle()->set($this->string->substr($title, 0, 60));



        /**
        * Changing meta description of PDP; Allowing only 60 characters
        * @param $product
        */
        private function alterMetaDescription($product)

        $pageConfig = $this->resultPage->getConfig();
        $description = $product->getMetaDescription();

        if ($description)
        $pageConfig->setDescription($this->string->substr($description, 0, 60));
        else
        $pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 60));





        As you can see, we wrote an around plugin for the method prepareAndRender(). Why we are relying on an around plugin is because we need the parameter $resultPage of the method in order to alter the meta title and description. Also, we need $product instance in order to retrieve the meta title and description. Product initialization is also happening inside prepareAndRender() method and hence will be obtained through the registry.



        You can customize the methods alterMetaTitle() and alterMetaDescription() as per your need. Currently, it will enforce 60 character limit to both meta title and description.



        I hope that gives you an idea.



        Note: Module creation and usage of the plugin is out of scope concepts as per this question. If you need to know more details, please refer devdocs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 15 at 8:38









        Rajeev K TomyRajeev K Tomy

        14.7k5 gold badges45 silver badges92 bronze badges




        14.7k5 gold badges45 silver badges92 bronze badges



























            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%2f282052%2ftruncate-meta-title-and-meta-description-in-magento2%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

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

            Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

            Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?