How to trim Meta Description tag value generated by Metatag module, to max 160 characters

October 07, 2017

Introduction

I was using a drupal website, and using metatag module to have all nice SEO metatags in my page. The content type is of article type.

I was using On page optimization of the article pages, and found that meta description value was exceeding the maximum 160 character limit. Then, my struggle started to find a way to limit this description limit. And, I found a solution.

More Context

To give you more context to my problem, I was using "Long text and Summary" type of widget in Body field of Article content type. Which has a summary field.

And, in my metatag setting, I have set the value of Meta description to:[node:summary] token. Which was perfectly being replaced with the content of my article text. But, the problem was that it was coming out way more than 160 characters.

The drupal code actually put everything till first text break(paragraph break) into summary field.

My Vision of Solution

I didn’t want to write custom code for this, which I could. But, drupal best practices says, that we should not change code of Core modules. Rather do an override kind of stuff. So, I was looking for some hooks, and override methods which can help me.

My Struggle Started

So, the modules in my target are:
  • Field (Core module)
  • Metatag
  • Token
  • Node (Core module)

1. Field Module

I found the code where the field "Long text with summary" was placed. But, it was just have settings to render widget, and some validation

2. Metatag

I searched for the code who was filling the token: "node:summary". Couldn't find much.

3. Token

Then, looked for how this token was exposed. This leads me to node module.

4. Node

I did a grep of "node:summary", I just found something in test files. Then, later I found the file: node.tokens.inc, which was exposing the tokens, and was responsible in filling the values there.

I saw this code for summary:

$trim_length = $instance['display']['teaser']['settings']['trim_length'];

So, this shows that the code was getting the max length value from teaser display settings.

Solution

Goto: Content type -> Article -> Manage Display -> Teaser (on top right) ![How to trim Meta Description tag value generated by Metatag module, to max 160 characters](./xdrupal-content-type.png.pagespeed.ic.C5Rrl6E4x7.png)

Then, click on setting button for Body. In my case, it was 600. I changed it to 160. And, Voila.

To test, just view the pages. You will not need to update(edit/save) them.

Enjoy.


Similar Posts

Latest Posts