How to correctly hook your filter to the post content
I’m seeing a lot of plugins that just hook themselves on the the_content filter tag, without bothering to handle excerpts.
Because get_the_excerpt() / the_excerpt() will call get_the_content() if the post doesn’t have an excerpt set, in many cases you get your filter applied on excerpts as well. This can lead to unwanted text inside them, like escaped javascript code.
If you don’t want your filter to be applied to excerpts, then remove it during the time in which the excerpt is being generated. Here’s one way to do it:
add_filter('the_content', 'my_plugin_filter');
// remove our filter, as early as possible
add_filter('get_the_excerpt', function($text){
remove_filter('the_content', 'my_plugin_filter');
return $text;
}, -999);
// add it back after wp_trim_excerpt was applied, in case the theme calls the_content after
add_filter('get_the_excerpt', function($text){
add_filter('the_content', 'my_plugin_filter');
return $text;
}, 999);
Another way is to check if the excerpt is being processed within your filter:
add_filter('the_content', 'my_plugin_filter'); function my_plugin_filter($text){ if(in_array('get_the_excerpt', $GLOBALS['wp_current_filter'])) return $text; // not the excerpt, do your stuff here... return $text; }