Last active
January 23, 2018 12:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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