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'];
}
@robneu
Copy link
Author

robneu commented Dec 2, 2013

You can find a full explanation of this code and how to use it here: http://wpbacon.com/tutorials/truncate-wordpress-seo-breadcrumbs/

@wpexplorer
Copy link

@naturettl
Copy link

The tutorial is not working anymore. The link is broken. Any advice?

@fschroiff
Copy link

@naturetti

// truncate Yoast SEO breadcrumb title
add_filter('wp_seo_get_bc_title', function($link_text, $id) {
  if ( strlen( $link_text ) <= ($limit = 20) ) return $link_text;
  return substr( $link_text, 0, $limit ).'&hellip;';
}, 10, 2);

@GermontZ
Copy link

GermontZ commented Dec 8, 2017

A new warning shows up:
Notice: wp_seo_get_bc_title is deprecated since version WPSEO 5.8! Use wpseo_breadcrumb_single_link_info instead. in /home/username/public_html/wp-includes/functions.php on line 4089

is there anyone who can help? Thank you :)

@keefyhub
Copy link

@GermontZ

using the new filter will look something like this (I have changed it slightly from the example above)

function shorten_yoast_breadcrumb_title($link_info)
{
    $limit = 20;
    if (strlen($link_info['text']) > ($limit)) {
        $link_info['text'] = substr($link_info['text'], 0, $limit) . '&hellip;';
    }

    return $link_info;
}

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

@netlabit
Copy link

Hi,
can it also be done with yoast meta title?

@robinurislamsohan
Copy link

robinurislamsohan commented Jun 11, 2021

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

@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