Php 钩子替换页面';使用Wordpress中变量中包含的字符串创建内容

Php 钩子替换页面';使用Wordpress中变量中包含的字符串创建内容,php,wordpress,Php,Wordpress,我正在构建一个wordpress插件,在这个插件中,我必须在适当的时候呈现一些特定的内容,而不是页面 以下是当前情况(所有文件都在插件的根文件夹中): my-template.php <?php echo "hi world"; <?php the_content(); 下面是期望的结果: my-plugins-main-php-file.php //...logic to determine if the content of the page has to be render

我正在构建一个wordpress插件,在这个插件中,我必须在适当的时候呈现一些特定的内容,而不是页面

以下是当前情况(所有文件都在插件的根文件夹中):

my-template.php

<?php
echo "hi world";
<?php
the_content();
下面是期望的结果:

my-plugins-main-php-file.php

//...logic to determine if the content of the page has to be rendered...
function load_template(){
    return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');
//...logic to determine if the content of the page has to be rendered...
add_action('string_include_or_some_other_hook',"hi world");
//...logic to determine if the content of the page has to be rendered...
function load_template(){
    return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');

$string = "hi world!";
add_filter('the_content', function() use ($string){
    return $string;
});
是否有一个钩子或其他方法可以用来直接从字符串呈现一些wordpress内容


谢谢

感谢@ArtisticPhoenix,以下是我问题的答案:

my-plugins-main-php-file.php

//...logic to determine if the content of the page has to be rendered...
function load_template(){
    return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');
//...logic to determine if the content of the page has to be rendered...
add_action('string_include_or_some_other_hook',"hi world");
//...logic to determine if the content of the page has to be rendered...
function load_template(){
    return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');

$string = "hi world!";
add_filter('the_content', function() use ($string){
    return $string;
});
my-template.php

<?php
echo "hi world";
<?php
the_content();

filter
The_content
可能会起作用
The_content”filter用于在从数据库检索文章内容之后,以及在将其打印到屏幕之前,对文章内容进行过滤。在您的过滤器中,您可以使用GET变量切换内容等。否则,这些页面是否类似于外部PHP html页面?如果是这样的话,你可以做一个短代码,那么你所要做的就是在WP中添加一个页面,然后把短代码放进去。它仍然给你一些可湿性粉剂的选择,如添加页面到菜单等,用最少的工作量。创建页面,粘贴快捷码,保存。请澄清您的问题。“直接从字符串呈现一些wordpress内容?”你是什么意思?Wordpress使用模板php文件呈现所有内容。您重定向Wordpress以输出您选择的模板(正如您所做的那样),然后,无论您想要打印什么字符串,您都会在该模板内进行回显。我看不出有什么问题?@ArtisticPhoenix的想法是避免手动创建页面。我正在测试你的解决方案,谢谢@如果你想在模板中包含变量,你会怎么做?那可以解决我的问题。关于这个标题,我明白你的意思,我正试图找到一个更好的。