I have a widget that lists some posts, and it uses get_the_excerpt()
, which in turn uses wp_trim_excerpt()
becasue wp_trim_excerpt()
is registered by default at 'get_the_excerpt'.
Fine.
My posts are like:
<h1>Date first of march</h1><h2>Price 100$</h2>
wp_trim_excerpt() does strip away any html (fine, the reson is not to clip away any html tag), but this leaves the my post excerpt like:
Date first of marchPrice 100$
with two words stick together.
Now, I know this ain't a perfect world and I could live with it, but the customer noticed and I'm always curious.
So I decided to add a filter _before_ wp_trim_excerpt():
function forsailing2_excerpt_reformat($input) {
$output = str_replace ( '><' , '> IAMAWHITESPACE <' , $input );
trigger_error("forsailing2_excerpt_reformat<br>input: $input<br>output: $output<br>");
return $output;
}
add_filter( 'get_the_excerpt', 'forsailing2_excerpt_reformat', 9);
This doesn't work: my filter is called one time for each excerpt shown but $input and $output are blank and the excerpts are displayed as if the filter was not there. I also tryed with priority 11 (I can see with debug bar the original filter mounted at priority 10).
FIRST QUESTION is: what did I get wrong (about priorities? parameters passing?)
I then tryed another way and deregistered wp_trim_excerpt()
from 'get_the_excerpt' and register a similar function at the same place.
OK, I succedeed and obtained exactly what I wanted, BUT.
In the original wp_trim_excerpt()
there's a line
$text = strip_shortcodes( $text );
SECOND QUESTION: Why is that? Is there any core developer to explain to me why stripping the shortcodes instead than elaborating? There might be a technical reason. performance?
I actually need the shortcodes to be elaborated not only in posts but also in excerpts, because the very first line contains some data taken from somewhere else.
I substituted the stripping with
$text = do_shortcode( $text );
and it worked just like I needed.
I also inserted
$text = str_replace ( '><' , '> <' , $text );
at the right point and that inserted the whitespace I needed.
I reached what I need, but feel uneasy because I duplicated a lot of code, changing only two lines, and basically overridden a core function of which I sould take care from now on.
THIRD QUESTION: Isn't there a better way to achieve this?
I feel like that if I could add my own filters _before_ wp_trim_excerpt() I would be alright and future proof.
Thank you for your time