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:
- Is there another hook I can use that runs when you load a page, not just “the_content”? Or,
- 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 Answer
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?