可以用Wordpress中的变量中包含的字符串替换页面内容

时间:2019-02-08 22:01:42

标签: php wordpress

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

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

my-template.php

<?php
echo "hi world";

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');

这是预期的结果:

my-plugins-main-php-file.php

//...logic to determine if the content of the page has to be rendered...
add_action('string_include_or_some_other_hook',"hi world");

是否可以使用钩子或其他方法直接从字符串中渲染某些wordpress内容?

谢谢!

1 个答案:

答案 0 :(得分:2)

这是我的问题的解决方案,这要感谢@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');

$string = "hi world!";
add_filter('the_content', function() use ($string){
    return $string;
});

my-template.php

<?php
the_content();