0

I'm having an issue related to the_excerpt filter. For some reason, if the post being displayed in a posts listing contains only shortcodes, it will execute those shortcodes no matter what.

What I would like is to either display the content if it's an enclosing shortcode, or strip it out altogether if it's a self-closing one and return an empty string if that's the case.

I'm using this function to strip out all shortcodes (because strip_shortcodes($content)) also didn't work:

add_filter('the_excerpt', 'my_custom_excerpt' );
function my_custom_excerpt() {
  global $post;

  $post_excerpt       = $post->post_excerpt != "" ? $post->post_excerpt : strip_tags($post->post_content);
  $excerpt_more       = "...";
  $excerpt_length     = 30;
  $clean_excerpt      = strpos($post_excerpt, '...') ? current(explode($post_excerpt, $excerpt_more, '1')) : $post_excerpt;
  $clean_excerpt      = strip_shortcodes(preg_replace("/\[[\/]?[^\]]*\]/", "", $clean_excerpt));
  $excerpt_word_array = explode(' ', $clean_excerpt);
  $word_count         = count($excerpt_word_array);
  $excerpt_word_array = array_slice($excerpt_word_array, 0, $excerpt_length);
  if ($word_count > $excerpt_length) {
    $excerpt = implode(' ', $excerpt_word_array) . $excerpt_more;
  }
  else {
    $excerpt = implode(' ', $excerpt_word_array);
  }
  return $excerpt;
}

I have a custom shortcode that generates an image slider and after the html, it outputs a script tag with the slider init function (basic stuff).

In the post excerpt I'm seeing the javascript output - which is super strange.

Accessing the posts, I see the slider running as expected, the html structure looks fine.

Anyway.. if you have any pointers to why this is happening and possible fixes, it would be great.

PS : the code above works, the if statement is ok from my tests, the issue is only when the post contains only shortcodes is when things get weird. If I append something to the excerpt variable, then it works as expected and prints out only what i've appended.

So if I do

else {
        $excerpt = implode(' ', $excerpt_word_array)."something";
      }

I see the excerpt for the post that contains only my shortcode print out "something". Without it, as mentioned before, it prints the javascript content. Which again, is weird.

2

Generating the excerpt is quite a jumble of filters, so it's difficult to see precisely what is happening, but here is my guess:

WordPress attaches a default filter (see line 148) to get_the_excerpt, namely wp_trim_excerpt. As you can see from the latter function's source code, it starts with getting the content if it gets passed an empty string. Now, if this filter is executed after yours and there are only shortcodes in the post, your filter will pass an empty string and the default filter will simply ignore everything you have done and start generating its own excerpt. So you could try making sure your filter is executed later, by changing its priority:

add_filter ('the_excerpt', 'my_custom_excerpt', 1, 20);

Alternatively, you could remove that filter:

remove_filter('get_the_excerpt', 'wp_trim_excerpt', 10);
  • I thought about setting the priority at some point but was in the middle of testing several ideas at once. After the 2 days of digging broke me I totally forgot to try to set the priority. Thanks! :) – Mihai Apr 26 '18 at 14:28

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.