Instantly share code, notes, and snippets.

Embed
What would you like to do?
A WordPress filter to avoid widows in your titles
<?php
/**
* A WordPress filter to avoid widows in your titles
*/
function avoid_title_widows( $title ) {
// Find the last space.
$last_space = strrpos($title, ' ');
// Replace it with a non-breaking space.
if ( $last_space ) {
$title = substr($title, 0, $last_space) . '&nbsp;' . substr($title, $last_space + 1);
}
return $title;
}
add_filter('the_title', 'avoid_title_widows');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment