• Resolved boutros

    (@boutros)


    With the default WordPress Excerpt function. WordPress strips the header tags (e.g. h1 tag) but keeps the unformatted text between the h1 tag as part of the excerpt.

    Is there a way, with a custom code, to Not only strip the h1 tag but also remove the text within the tag, when displaying the excerpt.

    Can this be done?
    Any help is appreciated.
    Boutros.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter boutros

    (@boutros)

    For the above question, here is the code so far:

    <?php
    function wp_strip_header_tags( $excerpt ) {
    $regex = '#(<h([1-6])[^>]*>)\s?(.*)?\s?(<\/h\2>)#';
    return str_replace($regex,'', $excerpt);
    }
    add_filter( 'the_excerpt', 'wp_strip_header_tags');
    ?>

    The problem here is that the $regex does its job, it returns an empty string for the header tag and its content when tested here: https://www.solmetra.com/scripts/regex/

    But, when I add the above code to my WordPress theme. The function does not work, meaning the header’s un-formatted content still displays
    in the excerpt. It is like this function is not hooked properly or is being disregarded.
    I tried the parameter ‘get_the_excerpt’ instead of ‘the_excerpt’ in the add_filter() function, with no difference to the output.
    Any ideas? Thanks.

    Thread Starter boutros

    (@boutros)

    Correction to the above code:
    Instead of str_replace(), I meant to use preg_replace().
    But still the result is the same.

    it seems that the filter ‘the_excerpt’ gets called too late; i.e. when the filter ‘the_excerpt’ is called, the tags are already stripped from the content.

    try to use the filter on ‘get_the_excerpt’ with a priority so that your function gets called before wp_trim_excerpt()

    untested –
    for some background, see /wp-includes/post-template.php lines 248++:

    function the_excerpt() {
    	echo apply_filters('the_excerpt', get_the_excerpt());
    }

    and a few lines further down, lines 270++:

    }
    
    	return apply_filters('get_the_excerpt', $output);
    }

    and /wp-includes/default-filters.php, lines 142++:

    add_filter( 'the_excerpt',     'wptexturize'      );
    add_filter( 'the_excerpt',     'convert_smilies'  );
    add_filter( 'the_excerpt',     'convert_chars'    );
    add_filter( 'the_excerpt',     'wpautop'          );
    add_filter( 'the_excerpt',     'shortcode_unautop');
    add_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );

    and /wp-includes/formatting.php, lines 2028++:

    function wp_trim_excerpt($text = '') {

    and lines 2054++:

    function wp_trim_words( $text, $num_words = 55, $more = null ) {

    and lines 3035++:

    function wp_strip_all_tags($string, $remove_breaks = false) {

    Thread Starter boutros

    (@boutros)

    Thanks alchymyth.
    I got your point with priority, but even when I use instead of the above line of code:

    add_filter( 'get_the_excerpt', 'wp_strip_header_tags', 1);

    I still get the same results. It did not make any difference. What priority should I be using?

    the code only seems to work on the hand-written excerpt;

    this virtually re-written wp_trim_excerpt() to use as the filter function seems to get this to work;

    https://pastebin.com/XMJNtH6A

    (not too widely tested; seems to work on automatic and handwritten excerpts; if the stripped content is shorter than the excerpt limit – even if the ‘normal’ text would be longer – the ‘read-more’ does not show)

    Thread Starter boutros

    (@boutros)

    Thanks alchymyth for your input.
    Your code works. I’ll work on it, test it and refine it some more.
    I knew I had to add the other pieces (length, more, etc.) to the code to make it to work.
    It would be nice if the top code (the simpler one) works without adding all the extra baggage.
    I appreciate all your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove Content within the Header tags in the Excerpt’ is closed to new replies.