Making WordPress.org


Ignore:
Timestamp:
01/12/2018 09:21:12 PM (6 years ago)
Author:
obenland
Message:

WordPress.tv: Roll our own widont.

On all index/archive screens, titles don't have a lot of room. The built-in minimum of 10 chars is not too helpful for this theme—let's see how 8 will fare.

Fixes #2106.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.tv/public_html/wp-content/themes/wptv2/functions.php

    r6374 r6380  
    870870    }
    871871}
     872
     873/**
     874 * Eliminates widows in strings by replace the breaking space that appears before the last word
     875 * with a non-breaking space.
     876 *
     877 * @link http://www.shauninman.com/post/heap/2006/08/22/widont_wordpress_plugin Typesetting widows
     878 *
     879 * @param string $str Optional. String to operate on.
     880 * @return string
     881 */
     882function wptv_widont( $str = '' ) {
     883    // Don't apply on non-tablet mobile devices so the browsers can fit to the viewport properly.
     884    if (
     885        function_exists( 'jetpack_is_mobile' ) && jetpack_is_mobile() &&
     886        class_exists( 'Jetpack_User_Agent_Info' ) && ! Jetpack_User_Agent_Info::is_tablet()
     887    ) {
     888        return $str;
     889    }
     890
     891    // We're dealing with whitespace from here out, let's not have any false positives. :)
     892    $str = trim( $str );
     893
     894    // If string contains three or fewer words, don't join.
     895    if ( count( preg_split( '#\s+#', $str ) ) <= 3 ) {
     896        return $str;
     897    }
     898
     899    // Don't join if words exceed a certain length: minimum 5 characters, default 15 characters, filterable via `widont_max_word_length`.
     900    $widont_max_word_length = max( 8, absint( apply_filters( 'widont_max_word_length', 8 ) ) );
     901    $regex = '#\s+([^\s]{1,' . $widont_max_word_length . '})\s+([^\s]{1,' . $widont_max_word_length . '})$#';
     902
     903    return preg_replace( $regex, ' $1&nbsp;$2', $str );
     904}
     905remove_filter( 'the_title', 'widont' );
     906add_filter( 'the_title', 'wptv_widont' );
Note: See TracChangeset for help on using the changeset viewer.