Last active
October 8, 2024 19:05
-
-
Save robneu/7743769 to your computer and use it in GitHub Desktop.
Truncate the title of Yoast's WordPress SEO breadcrumbs.
@robinurislamsohan
would it be possible to limit only the yoast variable title?
Improved @keefyhub code to avoid cutting UTF8 chars in half (and avoid breadcrumbs like "sin�…").
function shorten_yoast_breadcrumb_title($link_info)
{
$limit = 32;
if (strlen($link_info['text']) > ($limit)) {
$link_info['text'] = substr(
$link_info['text'],
0,
strpos($link_info['text'], ' ', $limit) // added: cut at the first white space after limit
) . '…'; }
return $link_info;
}
add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);
Hi,
can it also be done with yoast meta title?@lealustore There's nothing impossible when it comes to coding... 😉😉😉
Here's the function to truncate Yoast Meta Title:
function limit_title_yoast( $str ) { return substr($str, 0, 65); //Replace number 65 with your desired character } add_filter( 'wpseo_title', 'limit_title_yoast' );
This doesn't work now.
Improved @keefyhub code to avoid cutting UTF8 chars in half (and avoid breadcrumbs like "sin�…").
function shorten_yoast_breadcrumb_title($link_info) { $limit = 32; if (strlen($link_info['text']) > ($limit)) { $link_info['text'] = substr( $link_info['text'], 0, strpos($link_info['text'], ' ', $limit) // added: cut at the first white space after limit ) . '…'; } return $link_info; } add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);
Its working on the latest version of Yoast
@vj009007 - Just use the core WordPress wp_trim_words() function which takes care of all that for you ;)
https://developer.wordpress.org/reference/functions/wp_trim_words/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@robinurislamsohan
good, if i want to limit only post title variable on meta title? can it also be done ?