Calling get_the_excerpt from a filter on the_content kills shortcodes
-
I am writing a plugin that inserts some content after the_content. So I am doing this by adding a filter to the_content. As part of this, I need to get the summary of the post. But if I call get_the_excerpt from within the filter, then shortcodes in the_content stop being parsed.
When a page / post is viewed, shortcodes will show up as unprocessed shortcodes.
add_filter('the_content', 'myTest'); function myTest($content){ //content filters are run on get_the_excerpt, so we must remove our filter before calling it to avoid an infinite loop remove_filter('the_content', 'myTest'); $summary=get_the_excerpt(); add_filter('the_content', 'myTest'); return $content.'<p>Content edited</p>'; }
Shortcodes can be made to work by using
wp_trim_words
instead ofget_the_excerpt
, e.g.add_filter('the_content', 'myTest'); function myTest($content){ global $post; $summary=$post->post_excerpt ? $post->post_excerpt : wp_trim_words($content,55); return $content.'<p>Content edited</p>'; }
So, two questions:
- Why does this happen?
- Is using wp_trim_words in this case a suitable alternative?
Thanks
Dave
Viewing 14 replies - 1 through 14 (of 14 total)
Viewing 14 replies - 1 through 14 (of 14 total)
- The topic ‘Calling get_the_excerpt from a filter on the_content kills shortcodes’ is closed to new replies.