我正在制作 wordpress 插件,我想使用短代码在帖子中插入一些相当大的代码。我得到了这个简单的代码来模拟我的问题
function shortcode_fn( $attributes ) {
wanted();
return "unwanted";
}
add_shortcode( 'simplenote', 'shortcode_fn');
function wanted(){
echo "wanted";
}
并发布此内容
start
[simplenote]
end
这给出了这个结果:
wanted
start
unwanted
end
我希望它在开始和结束之间插入“想要的”文本。
我知道最简单的解决方案是在想要的()中返回“通缉”,但我已经拥有所有这些功能,而且它们非常庞大。没有从头开始编写所有内容的简单解决方案吗?
@edit:也许有什么方法可以将函数的所有 echo 存储在字符串中而不打印它?
最佳答案
一个简单的解决方法是使用 Output control职能:
function shortcode_fn( $attributes ) {
ob_start(); // start a buffer
wanted(); // everything is echoed into a buffer
$wanted = ob_get_clean(); // get the buffer contents and clean it
return $wanted;
}
关于php - Wordpress 短代码在错误的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334359/