Skip to content

Instantly share code, notes, and snippets.

@pixeline
Last active January 23, 2018 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pixeline/e0bfa7ac8cab2f4f3f36c8611f34895a to your computer and use it in GitHub Desktop.
Save pixeline/e0bfa7ac8cab2f4f3f36c8611f34895a to your computer and use it in GitHub Desktop.
Function to prevent orphans in WordPress post titles. It simply replaces the last space after the penultimate word of a title by a non-breaking space. Pfew.
function add_nonbreaking_space_to_text($title, $id = null){
$title = explode(' ', $title);
$length = count($title);
if( $length >0){
$title[ $length-2 ] .= " " . $title[ $length-1 ];
array_pop($title);
}
return implode(" ", $title);
}
// Hook the function to the title
add_filter('the_title', 'add_nonbreaking_space_to_text', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment