How to strip header tags & shortcodes from the_excerpt()
I'm trying to learn how to strip "header tags" "shortcodes" from the_excerpt()
... Searched for an answer to no avail so thought Id ask the community.
Thanks for your time
Best Shaun
I'm trying to learn how to strip "header tags" "shortcodes" from the_excerpt()
... Searched for an answer to no avail so thought Id ask the community.
Thanks for your time
Best Shaun
First, try this on your functions.php
:
add_filter( 'get_the_excerpt', 'strip_shortcodes', 20 );
If it doesn't work then try this edit.
echo strip_shortcodes( get_the_excerpt() );
In case the shortcode is not registered with WordPress function add_shortcode
add_filter( 'the_excerpt', 'remove_shortcodes_in_excerpt', 20 );
function remove_shortcodes_in_excerpt( $content){
$content = strip_shortcodes($content);
$tagnames = array('box', 'alert'); // add shortcode tag name
$content = do_shortcodes_in_html_tags( $content, true, $tagnames );
$pattern = get_shortcode_regex( $tagnames );
$content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
return $content;
}
Add this code to your functions.php
function wp_strip_header_tags( $text ) {
$raw_excerpt = $text;
if ( '' == $text ) {
//Retrieve the post content.
$text = get_the_content('');
//remove shortcode tags from the given content.
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$regex = '#(]*>)\s?(.*)?\s?()#';
$text = preg_replace($regex,'', $text);
/***Change the excerpt word count.***/
$excerpt_word_count = 60; //WP default is 55
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
/*** Change the excerpt ending.***/
$excerpt_end = '[...]'; //This is the WP default.
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
$excerpt = wp_trim_words( $text, $excerpt_length, $excerpt_more );
return apply_filters('wp_trim_excerpt', $excerpt, $raw_excerpt);
}
add_filter( 'get_the_excerpt', 'wp_strip_header_tags', 5);
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.