php - Ignore wordpress shortcode when outputting a limited word count -


i have created own custom word limit function in wordpress. need way ignore shortcode part of word count. i don't want strip out ignore shortcodes part of word count. otherwise if choose number 15 , shortcode in part of 15 word limit page fatal error.

function my_word_limit($limit) {     $content = explode(' ', get_the_content(), $limit);             if (count($content)>=$limit) {                     array_pop($content);                     $content = implode(" ",$content).'...';             } else {                     $content = implode(" ",$content);             }     $content = apply_filters('the_content', $content);      return $content;     } 

this shortcode using example.

[di-video-logged-out]<iframe src="https://www.youtube.com/embed/leiu8gba634" width="854" height="480"></iframe>[/di-video-logged-out] 

trim content , limit count word

( keep shortcode on output content trimmed, can tweak if don't )

here approach use wp_trim_word of content , filter wp_trim_word. also, can use function wpso36236774_trim_words such $post->post_content or get_the_content directly ( without filter ). usage commented inside code.

add_filter( 'wp_trim_words', 'wpso36236774_trim_words', 10, 4 ); /* trims text number of words.  * @author jevuska  * @version 1.0.1  *  * kepp shortcode if exist in text.  * combination function of strip_shortcodes , wp_trim_words  * default $num_words = 55  *  ** usage  ** using directly  ** wpso36236774_trim_words( $text, 56 )  ** wpso36236774_trim_words( $text, 56, null, false, false, true ) - return array  ** shortcode hidden if $num_words not set or if set value = 55 4 arguments  **  ** use wp_trim_words  ** wp_trim_words( $text, $num_words = 56 )  ** fire wp_trim_words  ** shortcode hidden if $num_words not set or $num_words = 55  ** position in bottom  ** add_filter( 'wp_trim_words', 'wpso36236774_trim_words', 10, 4 );  *  * @param  string  $text             text trim.  * @param  int     $num_words        number of words trim text to. default 5.  * @param  string  $more             optional string append end of trimmed text, e.g. &hellip;.  * @param  string  $original_content text before trimmed.  * @param  mix     $pos              shortcode position. can set 'top' value if using directly  * @param  boolean $count            word count  * @return string  text after filter witch $num_words  * @return array   if using directly , parameter $count set true  */ function wpso36236774_trim_words( $text, $num_words = 55, $more = null, $original_content = false, $pos = false, $count = false ) {     if ( null === $more)         $more = ' ' . '[&hellip;]';      $shortcode = $strip_shortcode = true;      if ( ! $original_content )         $original_content = $text;      $text = $original_content;      /* check existing shortcode      *      */     if ( false === strpos( $text, '[' ) )         $strip_shortcode = false;      global $shortcode_tags;      if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) )         $strip_shortcode = false;      /* strip content shortcode      *      */     if ( $strip_shortcode )     {         preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $text, $matches );         $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );          if ( ! empty( $tagnames ) )         {             $text = do_shortcodes_in_html_tags( $text, true, $tagnames );             $pattern = get_shortcode_regex( $tagnames );             preg_match_all( "/$pattern/", $text, $match );             if ( ! empty( $match[0] ) && is_array( $match[0] ) )             {                 $shortcode = '';                 $length    = count( $match[0] );                 ( $i = 0 ; $i < $length; $i++ )                     $shortcode .= do_shortcode( $match[0][ $i ] ); //match shortcode             }              $text = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $text );             $text = unescape_invalid_shortcodes( $text );         }     }      /* hide shortcode      * base on count function arguments      *      */     if ( func_num_args() == 1 || ( func_num_args() == 4 && 55 == $num_words ) )          $shortcode = '';      /* split content array words      *      */     $text = wp_strip_all_tags( $text );      /*      * translators: if word count based on single characters (e.g. east asian characters),      * enter 'characters_excluding_spaces' or 'characters_including_spaces'. otherwise, enter 'words'.      * not translate own language.      */     if ( strpos( _x( 'words', 'word count type. not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) )     {         $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );         preg_match_all( '/./u', $text, $words_array );         $limit_words_array = array_slice( $words_array[0], 0, $num_words + 1 );         $full_words_array  = $words_array[0];         $sep = '';     }     else     {         $limit_words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, preg_split_no_empty );         $full_words_array  = explode( ' ', preg_replace( "/[\n\r\t ]+/", ' ', $text ) );         $sep = ' ';     }      /* check word count base on $num_words      *      */     $word_count = count( $full_words_array );     if ( $word_count >= $num_words )     {         array_pop( $limit_words_array );         $text  = implode( $sep, $limit_words_array );         $text .= $more;          /* keep shortcode if exists , set position ( top or bottom text )          *          */         switch( $pos )         {             case 'top' :                 $text = $shortcode . $text;                 break;              default :                 $text .= $shortcode;                 break;         }     }     else     {         $text = apply_filters( 'the_content', $original_content );     }      if ( $count )         return array(             'text'  => $text,             'count' => $word_count         );      return $text; //output } 

let me know if issue code. tweak need , hope helps.

update

  • fix multiple shortcode
  • hide shortcode option base on total argument
  • patch $pos additional shortcode position top , bottom text ( func directly ). positon, must set class , css shortcode.

Comments