Cannot strip JW Player shortcode?

On the WordPress archive template I am using a custom excerpt function that includes a reference to strip_shortcodes(). For some reason, shortcodes are being rendered and displayed in the archive.

Example: http://arisehub.org/blog/page/2/

Scroll down to “Sing a New Song with Joshua Cunningham”

Notice the script code showing up. That’s a shortcode reference to JW Player, showing up rendered.

Here’s the code I’m using for the custom excerpt function.

function custom_excerpt($length, $more_text) { 
    global $post;     
    $text = get_the_content(); 
    $text = strip_shortcodes( $text );    
    $text = apply_filters('the_content', $text);  
    $text = str_replace(']]>', ']]>', $text);  
    $text = strip_tags($text, '<a>, <p>, <strong>, <em>, <b>');


    if(!empty($length)){
        $excerpt_length = apply_filters('excerpt_length', $length); 
    } else {
        $excerpt_length = apply_filters('excerpt_length', 180); 
    }

    if(!empty($more_text)){
        $excerpt_more = apply_filters('excerpt_more', ' ' . '&hellip; <br /><a href="'.get_permalink($post->id).'" class="more-link">'.$more_text.'</a>');
    } else { 
        $excerpt_more = apply_filters('excerpt_more', ' ' . '&hellip;<a href="'.get_permalink($post->id).'" class="more-link">+ more</a>');
    }

    $words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );  
    if ( count($words) > $excerpt_length ) {  
        array_pop($words);  
        $text = implode(' ', $words);  
        $text = $text . $excerpt_more;  
    } else {  
        $text = implode(' ', $words);  
    }  
    $output="<p>".$text.'</p>';
    echo $output;  
} 

Any ideas?

5 Answers
5

JW Player Plugin for WordPress does not register its shortcode like all other shortcodes, so strip_shortcodes() will not know about it and not strip it. In the code there is a note that this is because it uses argument names with a . in it, and WordPress does not support this.

There are probably multiple ways to solve this, but I would copy the relevant line from strip_shortcodes() and integrate the regex from the plugin:

function custom_excerpt( $length, $more_text ) {
    global $post;
    $text = get_the_content();
    $text = strip_shortcodes( $text );
    // Strip JW Player shortcode too
    $text = preg_replace( '/(.?)\[(jwplayer)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/s', '$1$6', $text );
    $text = apply_filters('the_content', $text);
    // Continue with the rest of your function ...

Leave a Comment