1

I am trying to filter the_content() on single page, but it looks like it is creating an infinite loop. I want to know how to fix this. I want to override the_content().

Is there any other way to override single page content from plugin?

add_filter( 'the_content', 'change_single_content' );
function change_single_content($content){
    global $post;

    if ( 'my-CPT' == get_post_type() && is_single() ){
        ob_start();
        include my_plugin_theme('single-template.php'); //include single template content
        return ob_get_clean();
    }

    return $content;
}
  • What plugin are you referring to? – Brad Dalton Feb 8 '14 at 13:07
  • 1
    Can you please add the code from my_plugin_theme() and tell us in an edit what single-template.php is and where it is from (theme, custom plugin, etc.)? – kaiser Feb 8 '14 at 13:26
2

My guess is that single-template.php uses the_content. Now think about what happens:

  1. Your filter causes single-template.php to load
  2. single-template.php uses the_content
  3. So single-template.php loads again
  4. Which uses the_content
  5. Which loads single-template.php
  6. Which uses the_content
  7. ...

I am not entirely sure how to fix that, as your question is light on detail. It is hard to tell what most of the code does or what your ultimate goal is, but it may be as simple as removing the filter conditionally:

add_filter( 'the_content', 'change_single_content' );
function change_single_content($content){
    global $post;

    if ( 'my-CPT' == get_post_type() && is_single() ){
        remove_filter( 'the_content', 'change_single_content' ); // this !!
        ob_start();
        include my_plugin_theme('single-template.php'); //include single template content
        return ob_get_clean();
    }

    return $content;
}

I am also pretty sure that what you are doing is the wrong way, or at least a less-right way, to do what you are trying to do. I can't prove that, but my spider sense is tingling something awful.

  • thanks, but I got which is causing problem. there is a while loop in including file. when I remove while it loads. – user1983017 Feb 9 '14 at 4:52
0
add_filter( 'the_content', 'filter_content' );
function filter_content( $content ){
    if ( is_single() ){
        $new_content = str_replace( 'for', 'FOR', $content );
        return $new_content;
    }
    else
    {
        return $content;
    }
}

Try this. I have tried replacing the_content on single post. It's replacing but not on index page because of is_single() condition.

You can tweak it that suits your need.

The problem with you code is that you are accepting $content as parameter and returning ob_get_clean(). You should return $content after tweaking it, in this scenario.

As in the code above, I have accepted $content, tweaked it and returned $new_content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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