How to properly replace the_content with the html in a php file?

I've built a plugin that needs to overwrite the content on a specific page. It works, but it places all head content within the body, after my own html. Is there a way to fix that?

add_filter('the_content', 'overwrite_content');
 
function overwrite_content($content) {
    if(is_page('Signup')){
        require_once(plugin_dir_path(__FILE__).'/views/signup.php');
    } else { 
        return $content;
    }
}

Topic php plugins Wordpress

Category Web


The reason you still see the content appearing after your HTML is because you are not returning anything in the first block of your if() statement. Obviously, you are outputting the HTML in the signup.php file, but you didn't override the original content. You can change your code to something like the following to see if you get the result you want:

function overwrite_content($content) {
  if (is_page('Signup')) {
    require_once(plugin_dir_path(__FILE__).'/views/signup.php');
    $content = '';
  }
  return $content;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.