• 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 that the_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 as the_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 remove wp_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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Yeah, what he’s doing isn’t explained that well. the_excerpt() eventually ends up calling wp_trim_excerpt() in certain cases to make an excerpt out of the post’s content. It is called by hooking into the ‘get_the_excerpt’ filter, which is a generic filter for altering the excerpt in any way that one wishes. Instead of merely trimming the excerpt, we can add our own filter callback to do whatever we like to the content passed.

    Since we are not satisfied with what wp_trim_excerpt() does, we can remove it from the process, then add in our own function.

    The code to alter WP functionality can basically reside in three places. The active theme’s functions.php, the active theme’s template files, or on your own site specific plugin. If you want your code to apply throughout the site, template files are a bad choice. Either functions.php or a plugin will work in this case. When altering theme files, you should create a child theme so your changes are protected when the theme is updated. Thus the code should be placed on the child theme’s functions.php.

    If you would like your code to function regardless of which theme is activated, then a plugin is the location for your code. A basic plugin is simple to create. It’s a good container for many of the little tweaks you may end up coding for your site.

    Thread Starter beenjamin

    (@beenjamin)

    A very clear explanation, bcworkz – thank you!

    So from what you’re saying I can place all of the code Pieter offers into functions.php (child-theme of course), with no need to delete or add any code manually from post-template.php and formatting.php?

    Moderator bcworkz

    (@bcworkz)

    That’s right! Filters are cool! You can alter core code without actually altering core code. Directly altering core code is a really bad practice, which is why there are so many filters and actions throughout WP core code.

    Thread Starter beenjamin

    (@beenjamin)

    I’ve learned a lot, bcworkz! Information I will surely use again and again in my WordPress journeys. Thanks for sharing your knowledge.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Confused where to place filters in the_excerpt() and get_the_excerpt()’ is closed to new replies.