0

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!

  • 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 May 4 '16 at 5:09
0

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?

  • 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. – real_big_words May 4 '16 at 12:22
  • Either way, my solution should work - try it and let me know. – TheDeadMedic May 4 '16 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! – real_big_words May 4 '16 at 16:51

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.