Advertisement
Guest User

Untitled

a guest
Jun 30th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. function wp_strip_all_tags_breaks($string, $remove_breaks = false) {
  2.     $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  3.     $string = strip_tags($string, '<p>');
  4.  
  5.     if ( $remove_breaks )
  6.         $string = preg_replace('/[\r\n\t ]+/', ' ', $string);
  7.  
  8.     return trim( $string );
  9. }
  10.  
  11. function wp_trim_words_breaks( $text, $num_words = 55, $more = null ) {
  12.     if ( null === $more )
  13.         $more = __( '&hellip;' );
  14.     $original_text = $text;
  15.     $text = wp_strip_all_tags_breaks( $text );
  16.     /* translators: If your word count is based on single characters (East Asian characters),
  17.        enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
  18.     if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
  19.         $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
  20.         preg_match_all( '/./u', $text, $words_array );
  21.         $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
  22.         $sep = '';
  23.     } else {
  24.         $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
  25.         $sep = ' ';
  26.     }
  27.     if ( count( $words_array ) > $num_words ) {
  28.         array_pop( $words_array );
  29.         $text = implode( $sep, $words_array );
  30.         $text = $text . $more;
  31.     } else {
  32.         $text = implode( $sep, $words_array );
  33.     }
  34.     return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
  35. }
  36.  
  37. function wp_trim_excerpt_breaks($text = '') {
  38.     $raw_excerpt = $text;
  39.     if ( '' == $text ) {
  40.         $text = get_the_content('');
  41.  
  42.         $text = strip_shortcodes( $text );
  43.  
  44.         $text = apply_filters('the_content', $text);
  45.         $text = str_replace(']]>', ']]&gt;', $text);
  46.         $excerpt_length = apply_filters('excerpt_length', 55);
  47.         $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  48.         $text = wp_trim_words_breaks( $text, $excerpt_length, $excerpt_more );
  49.     }
  50.     return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
  51. }
  52.  
  53. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  54. add_filter('get_the_excerpt', 'wp_trim_excerpt_breaks');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement