admin管理员组

文章数量:1198177

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;
    }
}

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;
    }
}
Share Improve this question edited May 30, 2022 at 8:41 Jan asked May 27, 2022 at 10:00 JanJan 11 bronze badge 1
  • 1 I'm bit nervous about require_once in case the the_content filter gets called twice for the same page load. Not that it should, though, but I think I've seen other questions where people were seeing that. – Rup Commented May 27, 2022 at 14:58
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}

本文标签: pluginsHow to properly replace thecontent with the html in a php file