NOTE PLEASE : for all of you who will mark this as a duplicate
THE ISSUE WAS NOT SOLVED USING CODE FROM THAT TOPIC. PLEASE DON'T MARK THIS AS A DUPLICATE BUT TRY TO HELP ME OUT. I'D REALLY APPRECIATE IT :)
Let me explain my issue.
I got a WordPress with 10,000 posts and like +6k of them have tittles longer than 65 characters which affects SEO.
I'm using this code to limit titles to 65 characters:
function tokenTruncate($title, $your_desired_width) {
$parts = preg_split('/([\s\n\r]+)/', $title, null, PREG_SPLIT_DELIM_CAPTURE);
$parts_count = count($parts);
$length = 0;
$last_part = 0;
for (; $last_part < $parts_count; ++$last_part) {
$length += strlen($parts[$last_part]);
if ($length > $your_desired_width) { break; }
}
return implode(array_slice($parts, 0, $last_part));
$your_desired_width = 65;
if (strlen($title) > $your_desired_width) {
$title = wordwrap($title, $your_desired_width);
$title = substr($title, 0, strpos($title, "\n")).'...';
return $title;
}
add_filter('the_title','tokenTruncate');