[英]Render file with shortcode into a wordpress plugin
I am relatively new to php and wordpress and I would like to know how I can render a php file without the include statement. 我对php和wordpress相对较新,我想知道如何在没有include语句的情况下呈现php文件。
For example if I have two files plugin.php
and component.php
例如,如果我有两个文件
plugin.php
和component.php
plugin.php plugin.php
<?php
add-shortcode('myshortcode', 'myshortcode-func');
function myshortcode-func()
// magic function that loads
$result = LOAD('component.php');
return $result;
}
?>
component.php component.php
<div>
<img scr="<?php getimage() ?>" />
</div>
NB I don't want to use include because I think it screws the rendering and insert the page in the flow when called. 注意我不想使用include,因为我认为它会在调用时拧紧渲染并在页面中插入页面。
Thanks for you help ! 谢谢你的帮助!
You can use an output buffer: 您可以使用输出缓冲区:
function myFunc(){
ob_start();
include('component.php');
return ob_get_clean();
}
How to: 如何:
$php = file_get_contents("component.php");
eval($php);
eval
is very dangerous though and shouldn't be used in production. 虽然
eval
非常危险,但不应该用于生产。
If this is for production, I'd recommend using hooks/filters (see wordpress source code). 如果这是用于生产,我建议使用钩子/过滤器(参见wordpress源代码)。 This lets you execute blocks of code on the fly, but is more constrained.
这使您可以动态执行代码块,但更受限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.