Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Created December 1, 2019 20:28
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 BinaryMoon/05f9f98d4d34fd28c1d9c60684016d0b to your computer and use it in GitHub Desktop.
Save BinaryMoon/05f9f98d4d34fd28c1d9c60684016d0b to your computer and use it in GitHub Desktop.
Widont
<?php
/**
* Add 'fix' for typographic widows
* This is the same code used on wordpress.com
*
* @param type $str
* @return string
*/
function theme_widont( $str = '' ) {
$str = rtrim( $str );
$space = strrpos( $str, ' ' );
$count = substr_count( $str, ' ' );
if ( $count <= 1 ) {
return $str;
}
if ( false !== $space ) {
$str = substr( $str, 0, $space ) . '&nbsp;' . substr( $str, $space + 1 );
}
return $str;
}
add_filter( 'the_title', 'theme_widont' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment