Advertisement
tpflanz

Allow HTML in excerpt (by default, WordPress strips the HTML

Mar 27th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. function custom_wp_trim_excerpt($text) {
  2. $raw_excerpt = $text;
  3. if ( '' == $text ) {
  4.     //Retrieve the post content.
  5.     $text = get_the_content('');
  6.  
  7.     $text = strip_shortcodes( $text );
  8.  
  9.     $text = apply_filters('the_content', $text);
  10.     $text = str_replace(']]>', ']]>', $text);
  11.  
  12.     $allowed_tags = '<p>,<a>,<em>,<strong>,<ul><ol><li>';
  13.     $text = strip_tags($text, $allowed_tags);
  14.  
  15.     $excerpt_word_count = 45;
  16.     $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
  17.  
  18.     $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
  19.     if ( count($words) > $excerpt_length ) {
  20.         array_pop($words);
  21.         $text = implode(' ', $words);
  22.         $text = $text . $excerpt_more;
  23.     } else {
  24.         $text = implode(' ', $words);
  25.     }
  26. }
  27. return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
  28. }
  29. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  30. add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement