0

Our website, many of the titles in meta name="title" content="HUGE MOTHER TITLES HERE..." Have titles exceeding the 50-55 norm. How to trim the meta title result only? I do not want to trim anything in the front end, only the rendered shown in the view src.

Saw this example below, but can dig up the needed action, the attached action with the below example may have been too old. Likewise this trims the 'chars' to 5 words, however, we would prefer to set the total string amount to max50, and then add ellipsis (...) to keep it more realistic.

<meta name="title" content="<?php echo wp_trim_words( get_the_title(), 5 ); ?>" />

Been digging thru the ancient posts of yesteryear (2015) online... nothing but voodoo...

Thanks guys.

  • Does this answer your question? [Truncate a multibyte String to n chars](https://stackoverflow.com/questions/2154220/truncate-a-multibyte-string-to-n-chars) – mickmackusa Dec 22 '20 at 10:40

1 Answers1

0

The below code trims the <meta name="title" content="it works here, booyakka..." /> to 50 characters and adds ... after the 50. Set the number to what you prefer.

However this solution does not trim the <title>Still monster titles in here...<title/> I will start a new question for that.

    /**
 * Fix Title Meta - add to functions.php
 */
function create_meta_title() {
$title = mb_strimwidth(get_the_title(), 0, 50, '...');
echo "<meta name=\"title\" content=\"$title\" />\n";
}
add_action('wp_head', 'create_meta_title');