solarbird: (vision)

WordPress’s default post excerpting is very, very stupid, and breaks HTML all the way back to 1.0 by ignoring newlines as whitespace. It’s terrible in other ways as well.

These are notes on how to fix that. There’s a very easy way, and varyingly easy ways to make custom code to do it your own way. I went with the very easy way because it is basically … hm. I’d say “exactly what I would have written” except it’s more capable and does some things I wouldn’t’ve bothered to do.

I think the most important thing I learned is that plugin function redefinitions override core function definitions even for your styles. This is in fact how you’re supposed to do that. Or you can do it in your style’s functions.php but unless you want it specific to one particular style it’s best to do it as a plugin.

Also there’s also a tool to make the plugin method even easier.


Key links:

EASIEST FIX:
There is an existing plugin! – https://wordpress.org/plugins/advanced-excerpt/

ON WRITING YOUR OWN – it’s not too hard:
https://wordpress.stackexchange.com/questions/141125/allow-html-in-excerpt/141136#141136

A PLUGIN TO MAKE WRITING YOUR OWN PLUGIN CODE TRIVIAL:
https://wordpress.org/plugins/code-snippets/


Helpful notes from Mastodon:

@cyberia@mast.eu.org helpfully said:

@moira Hi! If I understand your problem correctly, I guess you will have no choice but to overwrite the function rendering the excerpt ultimately.

Here is a proof of concept that should render what you expect – https://rentry.co/get_the_excerpt

[CODE COPIED FROM LINK]

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', function ($excerpt) {
global $post;
$excerpt = get_the_content('');
$excerpt = apply_filters('the_content', $excerpt);
$excerpt = strip_tags($excerpt, '<br>');
$excerpt = substr($excerpt, 0, 250);
return $excerpt;

});

[END CODE]

Full Disclaimer: please note that it is not suitable for production websites as it does not handle all situations! Bugs are to be expected with that. It’s only the path to follow imho…


@phoopee3@phpc.social helpfully said:

@moira @cyberia The best way to approach this is either, like you said, add code to your theme’s functions.php file, or to write a simple plugin for your site. With either of those approaches you would want to add a function to listen to the get_the_excerpt hook. A quick search online came up with this SO solution –

https://wordpress.stackexchange.com/questions/141125/allow-html-in-excerpt/141136#141136

I also found this plugin which might be the easiest solution if you don’t want to write code

https://wordpress.org/plugins/advanced-excerpt/


And @cyberia@mast.eu.org helpfully said:

You can add the snippet I wrote into your theme’s (or even better, your child theme’s) functions.php, or use a plugin like Code Snippets for a hassle-free, 30-second solution –

https://wordpress.org/plugins/code-snippets/

Feel free to let me know if you need any help!

Posted via Solarbird{y|z|yz}.

August 2025

S M T W T F S
     12
3 456789
10111213141516
17181920212223
24252627282930
31      

Most Popular Tags

Syndicate

RSS Atom