Confused where to place filters in the_excerpt() and get_the_excerpt()
-
So I’m trying to follow Pieter Goosen’s instructions on adding HTML formatting to excerpts but my limited knowledge of php functions is making it hard to understand Pieter’s first instructions in his solution (all the instructions before he gives the code to add to functions.php – not shown below). – I’ve been trying to work it out for hours and I feel like a dummy asking for help on this, but I just need to know in which php file, and where to make the changes he talks about.
Pieter’s Instructions
(original instructions here)COMPLETE GUIDE TO EXCERPTS
I’ve recently answered a few questions regarding excerpts, so I’ going to give a detailed explanation covering as much as I can.
HTML TAGS/FORMATTING
the_excerpt()
first of all doesn’t except any parameters, so nothing can be passed to it. In is a fact thatthe_excerpt()
trims the content to 55 words, and all html tags are stripped before returning the text.the_excerpt()
is located in wp-includes/post-template.php. To allow certain or all html tags in the excerpt, a new excerpt have to be created.First of all, the original function needs to be removed first, and then the new function needs to be hooked to
get_the_excerpt
. Please take note, this new excerpt will still be callable asthe_excerpt()
in template files, no need to change that.get_the_excerpt()
is located in wp-includes/post-template.php.The excerpt uses
wp_trim_excerpt
to return the trimmed text, so we need to removewp_trim_excerpt
first from the excerpt filter.wp_trim_excerpt()
is located in wp-includes/formatting.php, line 2355. This is how:remove_filter('get_the_excerpt', 'wp_trim_excerpt');
You can now add your new excerpt to get_the_excerpt
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
To allow html tags/formatting, we will need to specify which tags you will need to allow. You can use the following strip_tags statement to achieve that
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags());
Any help would be amazing!
Thank you in advance
- The topic ‘Confused where to place filters in the_excerpt() and get_the_excerpt()’ is closed to new replies.