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 );
}
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 );
}
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 );