WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question

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.

share|improve this answer
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?(<\/h\2>)#';
    return preg_replace(

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.

share|improve this answer
Thanks @m0r7if3r, your code is a step in the right direction. The regex variable need a ';' at the end (not a big deal). Also the $regex you defined does not seem to remove any text between headers. Noticed that your opening tag is h1-h6 but your closing tag is only h? Any ideas with the regex. Thanks. – Boutros AbiChedid Apr 17 '12 at 12:13
@BoutrosAbiChedid Closing tag is self-referential. \2 means the value of the second parenthesis, which in this case is ([1-6]). What this does is prevent <h1>foo <h3>bar</h3></h1> from turning into </h1> instead of to <h1>foo</h1> then to an empty string. It should be removing any text in between as well, that's what the .* is for. You can test your regexes here. – mor7ifer Apr 17 '12 at 12:19
thanks @m0r7if3r for the explanation. I tried the regex with the test tool and it works. But when I put your code above in the functions.php file of WordPress theme. It still shows the content between headers (but un-formatted). Please see this temporary example in my blog (first post) you see the default WordPress excerpt starting at: "header1 header2 header3 header4 header5 header6..." bacsoftwareconsulting.com/blog?wptheme=Paper Wall any ideas? Thanks. – Boutros AbiChedid Apr 17 '12 at 13:08
Try using get_the_excerpt instead of the_excerpt as your hook. – mor7ifer Apr 17 '12 at 13:12
I already did that, did not make any difference. Other ideas? Thanks. – Boutros AbiChedid Apr 17 '12 at 13:21

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);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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