Skip to content

Instantly share code, notes, and snippets.

@joemcgill
Created February 21, 2015 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemcgill/89629cd4ac3cd975db89 to your computer and use it in GitHub Desktop.
Save joemcgill/89629cd4ac3cd975db89 to your computer and use it in GitHub Desktop.
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