我正在制作wordpress插件,我想使用短代码在post中插入一些相当大的代码。我有一个简单的代码来模拟我的问题
function shortcode_fn( $attributes ) {
wanted();
return "unwanted";
}
add_shortcode( 'simplenote', 'shortcode_fn');
function wanted(){
echo "wanted";
}与此内容一起发布
start
[simplenote]
end这就得出了这个结果:
wanted
start
unwanted
end我希望它在开始和结束之间插入“想要的”文本。我知道最简单的解决方案是只返回想要的(),但是我已经有了所有这些函数,它们非常庞大。有没有一个简单的解决方案,而无需从头开始编写所有的东西?
@编辑:也许有什么方法可以在不用打印的情况下用字符串存储函数的所有回波?
发布于 2014-04-28 07:15:49
一个简单的解决方法是使用输出控制函数:
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;
}发布于 2014-04-28 07:43:14
遵循这个链接API#Overview
当显示the_content时,短代码API将解析任何已注册的短代码,如"myshortcode",分离并解析属性和内容(如果有的话),并传递相应的短代码处理程序函数。由短代码处理程序返回的任何字符串返回的(而不是回显)将被插入到post主体中,而不是该短代码本身。
因此,所有的短代码函数都必须返回,您可以将您的函数“想要”更改为:
function wanted(){
return "wanted";
}发布于 2022-07-20 16:09:33
使用拉斯兰·贝斯的答案,代码如下所示:
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;
}
add_shortcode( 'simplenote', 'shortcode_fn');
function wanted(){
echo "wanted";
}https://stackoverflow.com/questions/23334359
复制相似问题