stop shortcode stripping in category and archive pages

Using the WordPress default theme 2010 1.1, shortcodes work on the main blog page and individual post pages, but are being stripped out on category and archive pages.

For example, stick in the theme’s functions.php:

Read More
add_shortcode('tsc', 'tsc_process_shortcode' );

function tsc_process_shortcode($atts, $content = null) {
return 'INSERTED TEXT ' . $content;
}

INSERTED TEXT isn’t generated from [tsc] in post content shown on category and archive pages. How do I get shortcodes to function on category and archive pages?

Related posts

Leave a Reply

2 comments

  1. Shortcodes are stripped from the excerpt before filters are applied. Try something more like this:

    function tsc_execute_shortcodes_in_excerpts( $text, $raw_text ){
      if(empty($raw_text)){
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $words = preg_split("/[nrt ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
        if ( count($words) > $excerpt_length ) {
          array_pop($words);
          $text = implode(' ', $words);
          $text = $text . $excerpt_more;
        } else {
          $text = implode(' ', $words);
        }
      }
      return $text;
    }
    
    add_filter( 'wp_trim_excerpt', 'tsc_execute_shortcodes_in_excerpts', 10, 2 );
    

    That basically re-runs wp_trim_excerpt() on the content (minus shortcode stripping) if it was trimmed to begin with. If you want to target your shortcode only, you could do something like this after get_the_content(''):

    $text = str_replace( '[tsc', '{tsc', $text );
    $text = strip_shortcodes( $text );
    $text = str_replace( '{tsc', '[tsc', $text );
    

    Hope that helps.

  2. Assuming that you are using the default 2010 the archive and category loops use
    the_excerpt instead of the_content and that is why the shortcodes are not being generated, fortunately there is a simple fix for that , just add this line to your themes functions.php file

    add_filter( 'the_excerpt', 'do_shortcode', 11 );
    

    Hope this helps.