Remove Text within Header tags in the Excerpt

With the default WordPress Excerpt function. WordPress strips the header tags (e.g. h1 tag) but keeps the unformatted text, that lies within the opening/closing of the h1 tag, displayed as part of the excerpt.

Is there a way, in addition of striping the h1 tag to also completely remove the text within the tag, when displaying the excerpt?

Read More

Thanks.

Related posts

Leave a Reply

4 comments

  1. Rather than messing with regex, you might consider using a user-defined excerpt (i.e. a manual excerpt, which retains HTML tags, rather than relying solely on the automatic excerpt, which strips HTML tags.

    Depending on your specific use case, using the manual excerpt for displaying HTML-formatted excerpts is usually an easier approach.

  2. Here is a function that works for me.

    function bac_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);
    
             //Regular expression that strips the header tags and their content.
             $regex = '#(<h([1-6])[^>]*>)s?(.*)?s?(</h2>)#';
             $text = preg_replace($regex,'', $text);
    
             /***Change the excerpt word count.***/
             $excerpt_word_count = 55; //This is WP default.
             $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', 'bac_wp_strip_header_tags', 5);
    
  3. add_filter( 'the_excerpt', 'wpse49280_strip_header_tags', 1 );
    function wpse49280_strip_header_tags( $excerpt ) {
        // this is just an example, there is probably a better regex
        $regex = '#(<h([1-6])[^>]*>)s?(.*)?s?(</h2>)#';
        return preg_replace(
            $regex,
            '',
            $excerpt
        );
    }
    

    It should be noted that regexing HTML is not the best solution, but I think it’s the most practical for something simple such as this.

  4. Thanks @m0r7if3r for all your help. I don’t think I would have figured out the Regex part.
    This is the code I am using and it works:

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