0

I wrapped a shortcode around my post content and I have noticed it is making the excerpt blank on archive pages instead of auto generating one from the_content/post. At a loss as to what is causing it or what to try. Its not even outputting the shortcode in the_excerpt its just blank. I would have thought if there was going to be a problem it would have been the shortcode appearing as part of the_excerpt but its just blank full stop. All my predefined shortcode does is wrap some complex HTML5 tags around the content which an author would make mistakes doing on a regular basis for consistency in certain situations so its not a complex shortcode. My post is outputting perfectly with the_content. As soon as I remove the shortcode it works again. If I put the shortcodes back in and set a manual excerpt it works so its only when it has to auto generate the_excerpt from a post wrapped in this shortcode. Appreciate any ideas if anyone else has experienced something similar as I am at a loss to explain it or how to resolve.

Many thanks.

In simple/stripped back terms im wrapping for example this:

add_shortcode( 'ARTICLE_SECTION', 'trg_shortcode_article_section');
function trg_shortcode_article_section_run( $content ) {
  global $shortcode_tags;

  // Backup current registered shortcodes and clear them all out
  $orig_shortcode_tags = $shortcode_tags;
  remove_all_shortcodes();

  add_shortcode( 'ARTICLE_SECTION', 'trg_shortcode_article_section' );

  // Do the shortcode (only the one above is registered)
  $content = do_shortcode( $content );

  // Put the original shortcodes back
  $shortcode_tags = $orig_shortcode_tags;

  return $content;
}
// actual shortcode function
function trg_shortcode_article_section( $atts, $content = null ) {

    $atts = array();

    $output = '<section>';
    $output .= $content . '</section>';
    return $output;
}
add_filter( 'the_content', 'trg_shortcode_article_section_run', 7 );

In partial loop:

<p><?=trg_excerpt_article()?></p>

Ive put in to debug early right at start before anything else just:

function trg_excerpt_article(){
  echo '<!-- excerpt - ' . get_the_excerpt() . ' -->';
}

That returns blank as well.

  • 2
    I think I understand, but I agree it's an odd way of going about things. Why not just add this HTML5 into the template? – vancoder Mar 21 '13 at 23:40
  • Sorry I appreciate its quite hard without posting some code up so ive edited it with whats useful and stripped back to make sense. Tried a few more things but im still getting blank on the ones which ive wrapped the output in the shortcode. – Jimbo Mar 22 '13 at 0:22
  • Just to note I have also tried just the standard the_excerpt() in my partials/type-loop.php instead of trg_excerpt_article() which is also blank. – Jimbo Mar 22 '13 at 0:40
  • Ok im learning a bit more about it and when its happening. If I wrap all content in it it returns blank. Say its 10 paragraphs long and I wrap the first 9 the paragraph remaining unwrapped gets picked up as the excerpt. So its wiping everything wrapped in the tag from the_excerpt and if anything is outside of it becomes the_excerpt. Not sure it that helps but im unsure where to go with it. I never seem to have much joy with writing shortcodes. – Jimbo Mar 22 '13 at 1:02
  • 1
    Possible duplicate of Shortcode content does not show in feed discription/excerpt – kraftner Dec 3 '15 at 19:36
0

Excerpts remove all mark-up and shortcodes. Try adding the following into your functions.php:

add_filter('the_excerpt', 'do_shortcode');

This should enable shortcodes in your excerpt again. Bear in mind the generated HTML5 will be stripped out.

  • I gave this a whirl and ran the shortcode like I would on the_content but still returns blank. – Jimbo Mar 22 '13 at 0:22
  • "ran the shortcode like I would on the_content" - what does this mean? – vancoder Mar 22 '13 at 4:14
0

I have managed to fix the issue. Wordpress strip_shortcodes() wasnt cutting it.

It seems to me that Wordpress doesnt like a whole post to be wrapped in shortcodes and returns the excerpt blank. It also could be the altered timings that I have applied my shortcodes to the_content. However I believe it is the former having read also some others online experiencing this. For the excerpts I have stripped the shortcodes manually when detecting empty excerpts.

function my_custom_excerpt($length_callback='', $more_callback='') {

        if(function_exists($more_callback)){
            add_filter('excerpt_more', $more_callback);
        }

        if(function_exists($length_callback)){
            add_filter('excerpt_length', $length_callback);
        }

        $output = get_the_excerpt();

        if( empty( $output ) ){ 
            $excerpt = get_the_content();
            $excerpt = preg_replace('/\[(.*)\]/','',$excerpt);
            $excerpt = esc_attr( strip_tags( stripslashes( $excerpt ) ) );
            $excerpt = wp_trim_words( $excerpt, ( (function_exists($length_callback)) ? call_user_func($length_callback,55) : 55 ), ( (function_exists($more_callback)) ? call_user_func($more_callback) : '' ) );

            $output = $excerpt;
        }

        $output = apply_filters('wptexturize', $output);
        $output = apply_filters('convert_chars', $output);
        return $output;
}

Not sure yet if this is 100% fool proof but it seems to handle when a post is wrapped in shortcodes being returned empty and then proceeds to get the_content itself strips any shortcodes out and formats the excerpt as it should from there. Hopefully this helps others out who experience similar issues.

Thanks for the help and advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.