WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Insert Text after Break via add_filter (4 posts)

  1. kwatog
    Member
    Posted 1 year ago #

    I'm trying to add some text after the break (<!--more-->) but my add filter code doesn't work. Here's my code. What's the problem with it?

    add_filter( 'the_content', 'after_more_ads',0 );
    function after_more_ads( $content ) {
        if ( is_single() ){
             $content = preg_replace("/<!--more-->/"," <!--more--> insert test here<br/> ",$content);
          }
        // Returns the content.
        return $content;
    }

    thanks in advance

  2. Paul Clark
    Member
    Posted 1 year ago #

    the_content filter doesn't work in this situations for two reasons:

    Wrong Filter
    the_content is too late to modify the <!--more--> link in any situation, because the more link filter is converted to a span or a link inside get_the_content.

    See:

    Wrong Context (single post)
    In your specific case, the more link filter will also not work, because when is_single() is true, the more link filter isn't run at all. Instead, it's converted to a <span> at post-template.php:205.

    In this case, a couple of options are:

    1. Filter $post->content before the_content filter runs. Also use the better regex for the more tag from post-template.php:188.
    2. Use the same filter you have, but instead of searching for the more tag, search for the span with $post->ID created by post-template.php:205.
  3. kwatog
    Member
    Posted 1 year ago #

    thanks, pdclark. I realized that after reviewing the codes. Anyhow, here's my revised code

    add_filter( 'the_content', 'after_more_ads', 10 );
    function after_more_ads( $content ) {
        if ( is_single() ){
             $content = preg_replace("/<span id=\"more(.*?)span>/"," -- additional text here -- ",$content);
          }
        return $content;
    }

    somehow works.

  4. Paul Clark
    Member
    Posted 1 year ago #

    Great! Thanks for sharing your final solution for others to use in the future.

Topic Closed

This topic has been closed to new replies.

About this Topic