Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. function wpse_allowedtags() {
  2. // Add custom tags to this string
  3. return '<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<p>';
  4. }
  5.  
  6. if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
  7.  
  8. function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
  9. $raw_excerpt = $wpse_excerpt;
  10. if ( '' == $wpse_excerpt ) {
  11.  
  12. $wpse_excerpt = get_the_content('');
  13. $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
  14. $wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
  15. $wpse_excerpt = str_replace(']]>', ']]&gt;', $wpse_excerpt);
  16. $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
  17.  
  18. //Set the excerpt word count and only break after sentence is complete.
  19. $excerpt_word_count = 110;
  20. $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
  21. $tokens = array();
  22. $excerptOutput = '';
  23. $count = 0;
  24.  
  25. // Divide the string into tokens; HTML tags, or words, followed by any whitespace
  26. preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
  27.  
  28. foreach ($tokens[0] as $token) {
  29.  
  30. if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
  31. // Limit reached, continue until , ; ? . or ! occur at the end
  32. $excerptOutput .= trim($token);
  33. break;
  34. }
  35.  
  36. // Add words to complete sentence
  37. $count++;
  38.  
  39. // Append what's left of the token
  40. $excerptOutput .= $token;
  41. }
  42.  
  43. $wpse_excerpt = trim(force_balance_tags($excerptOutput));
  44.  
  45. $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Keep Reading: %s &nbsp;&raquo;', 'wpse' ), get_the_title()) . '</a>';
  46. $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
  47.  
  48. //$pos = strrpos($wpse_excerpt, '</');
  49. //if ($pos !== false)
  50. // Inside last HTML tag
  51. //$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
  52. //else
  53. // After the content
  54. $wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
  55.  
  56. return $wpse_excerpt;
  57.  
  58. }
  59. return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
  60. }
  61.  
  62. endif;
  63.  
  64. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  65. add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement