0

A while back (5+ years) I was asked to build a site where there was some HTML needed in the WP excerpt, from what I can remember I achived this by overriding "get_the_excerpt" something like the accepted answer here: Allow HTML in excerpt

This does not seem to work in the latest version of WP 6.2.2 (at time of writing), can someone point me in the right direction for allowing a few HTML tags to be included in the excerpt?

If I alter formatting.php > strip_all_tags as below, this gets me the result I am looking for, but obviously altering the core is not a solution, how would I achive the same thing in my child themes functions.php

function wp_strip_all_tags( $text, $remove_breaks = false ) {
    if ( is_null( $text ) ) {
        return '';
    }

    if ( ! is_scalar( $text ) ) {
        /*
         * To maintain consistency with pre-PHP 8 error levels,
         * trigger_error() is used to trigger an E_USER_WARNING,
         * rather than _doing_it_wrong(), which triggers an E_USER_NOTICE.
         */
        trigger_error(
            sprintf(
                /* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */
                __( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ),
                __FUNCTION__,
                '#1',
                '$text',
                'string',
                gettype( $text )
            ),
            E_USER_WARNING
        );

        return '';
    }

    $text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
    $text = strip_tags( $text, '<h3>, <div>, <span>' ); // TO DO OVERRIDE

    if ( $remove_breaks ) {
        $text = preg_replace( '/[\r\n\t ]+/', ' ', $text );
    }

    return trim( $text );
}
3
  • Are you using one of the default themes? Need to rule out the theme doing any extra work.
    – Caleb
    Commented Jun 6, 2023 at 14:52
  • 1
    In my testing, it seems that manual excerpts permit HTML, but auto-generated excerpts strip tags.
    – Caleb
    Commented Jun 6, 2023 at 14:58
  • We are using the Divi theme, and the posts are being created by gravity forms, I did contact gravity and they suggested they don’t do anything out of the ordinary the posts are just WP posts according to them.
    – Tbreak
    Commented Jun 7, 2023 at 9:16

1 Answer 1

0

To get started, you'll need to remove the wp_trim_excerpt function from the get_the_excerpt filter:

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10 );

This will result in no auto-generation of excerpts (manually entered excerpts will still show).

From here, add your own function and filter for generating the excerpt:

function trim_excerpt_with_html( $text = '', $post = null ) {
    ...
}
add_filter( 'get_the_excerpt', 'trim_excerpt_with_html', 10, 2 );
2
  • Thanks Caleb, I will give this a go.
    – Tbreak
    Commented Jun 7, 2023 at 9:17
  • I apologise, im totally php illiterate. Can you give me an example with what should i replace the 3 dots in the custom function?
    – Ilir
    Commented May 21 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.