Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active October 8, 2024 19:05
Show Gist options
  • Save robneu/7743769 to your computer and use it in GitHub Desktop.
Save robneu/7743769 to your computer and use it in GitHub Desktop.
Truncate the title of Yoast's WordPress SEO breadcrumbs.
<?php
/**
* Truncate the title of Yoast's WordPress SEO breadcrumbs.
*
* Sometimes you will have post titles which are longer than you want
* them to be in your breadcrumbs. This will allow you to set a maximum
* length for the post title used in Yoast's breadcrumb function.
*
* @author FAT Media, LLC
* @link http://wpbacon.com/tutorials/truncate-wordpress-seo-breadcrumbs/
*/
add_filter('wp_seo_get_bc_title', 'wpbacon_truncate_bc_title');
function wpbacon_truncate_bc_title() {
// Get the title of the current post.
$title = get_the_title();
// Determine the length of the title.
$title_length = strlen( $title );
// Set a limit for the breadcrumb title.
$limit = 55;
// Truncate the title.
$truncated = substr( $title, 0, $limit );
// Add an ellipsis if the title has been truncated.
if ( $title_length > $limit ) {
$truncated .= '...';
}
$link['text'] = $truncated;
return $link['text'];
}
@netlabit
Copy link

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' );

@robinurislamsohan
good, if i want to limit only post title variable on meta title? can it also be done ?

@netlabit
Copy link

@robinurislamsohan
would it be possible to limit only the yoast variable title?

@diego-betto
Copy link

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
    ) . '&hellip;'; }

    return $link_info;
}

add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);

@hetaelton
Copy link

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.

@vj009007
Copy link

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
    ) . '&hellip;'; }

    return $link_info;
}

add_filter('wpseo_breadcrumb_single_link_info', 'shorten_yoast_breadcrumb_title', 10);

Its working on the latest version of Yoast

@wpexplorer
Copy link

@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