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
1 Answer
Reset to default 0The 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
版权声明:本文标题:plugins - How to properly replace the_content with the html in a php file? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738565446a2100056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论