2

I want to be able to replace a page's content if it is a very specific page.

Here is what I've written so far:

function signup () {
    global $post;
    $slug = $post->post_name;

    if (is_page( ) && strcmp($slug, 'signup_slug') == 1) {
        $content = "New Text";
    } else {
        // This is literally just "the_content()" except returning the value, not echo
        $content = $post->post_content;
        $content = apply_filters( 'the_content', $content );
        $content = str_replace( ']]>', ']]>', $content );
    }
    return $content;
}

add_action('the_content', 'signup');

When I run this code on the page named "signup_slug", it works fine. Everywhere else, the "apply_filters" gives me an error that says something about an overflow error in functions.php and that it ran 100 times and then aborted.

If I remove the "apply_filters" bit, it acts kind of funky, making every post on the front page a minimum height that is sometimes larger than normal. I think it's ignoring my "Read More" tags. Plus, all of my YouTube links show up as a URL instead of an embedded video, like they normally do.

Basically, I was wondering 2 things:

  1. Is there another hook I can use that runs when you load a page, not just "the_content"? Or,
  2. Is there a way to display the contents of "the_content()" without all of the funky side effects that I've been having?

Or, an even better question might be, "What's the best way to create a hook that replaces a page's content?"

Thanks!

1
  • You should use add_filter instead of add_action. I think your if statement is the "Funky" bit. You can just do is_page('signup-slug') you dont need strcmp.
    – ngearing
    Commented May 4, 2016 at 5:09

1 Answer 1

3

Your function is signup but you're hooking check_for_signup. You're also trying to apply the_content filters from within a function that's hooked to the_content:

function wpse_225562_replace_for_signup( $content ) {
    if ( strcmp( 'signup_slug', get_post_field( 'post_name' ) ) === 0 ) {
        $content = 'Sign up, bitch.';
    }

    return $content;
}

add_filter( 'the_content', 'wpse_225562_replace_for_signup' );

Here we are simply hooking to the_content and replacing the passed-through $content with something else if the current page is signup_slug - hope that makes sense?

3
  • Sorry, I changed the function name for this question but forgot to change the filter. In the original code, the filter and the function name are the same. Plus, the filter is not called inside of the function. I've changed the indents so that it's easier to see this. Commented May 4, 2016 at 12:22
  • Either way, my solution should work - try it and let me know. Commented May 4, 2016 at 14:30
  • Yep, it worked! I needed to use filter and not action. Also, the equation should be "=== 0" and not "=== 1", but that was my mistake. Thank you! Commented May 4, 2016 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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