php Wordpress在简码函数中使用echo vs return
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21538180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress using echo vs return in shortcode function
提问by kabirbaidhya
I just found out that both echoand returnworks fine for displaying content from a shortcode function.
我刚刚发现两者echo并return正常工作从一个简码功能显示内容。
function foobar_shortcode($atts) {
echo "Foo Bar"; //this works fine
}
function foobar_shortcode($atts) {
return "Foo Bar"; //so does this
}
I just want to know, is there any difference between using either of these? If so what's the recommended one? I normally use echo in this case; is it okay?
我只想知道,使用这两者有什么区别吗?如果是这样,推荐的是什么?在这种情况下,我通常使用 echo;可以吗?
回答by Nathan Dawson
Echo may work in your specific case but you definitely shouldn't use it. Shortcodes aren't meant to output anything, they should only return content.
Echo 可能适用于您的特定情况,但您绝对不应该使用它。短代码并不意味着输出任何东西,它们应该只返回内容。
Here's a note from the codex on shortcodes:
以下是代码中关于短代码的注释:
Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.
请注意,短代码调用的函数不应产生任何类型的输出。短代码函数应返回用于替换短代码的文本。直接产生输出会导致意想不到的结果。
http://codex.wordpress.org/Function_Reference/add_shortcode#Notes
http://codex.wordpress.org/Function_Reference/add_shortcode#Notes
回答by Muhammad Mehran Khan Attari
If you are outputting a lot of contents, then you should use:
如果您要输出很多内容,那么您应该使用:
add_shortcode('test', 'test_func');
function test_func( $args ) {
ob_start();
?> <your contents/html/(maybe in separate file to include) code etc> <?php
return ob_get_clean();
}
回答by Kimberly Fox
If you use "echo" in the shortcode, the information will show up wherever the shortcode is processed, which isn't necessarily where you actually added the shortcode. If you use "return", the information will return exactly where you added the shortcode within the page.
如果您在短代码中使用“echo”,信息将显示在处理短代码的任何地方,这不一定是您实际添加短代码的地方。如果您使用“返回”,信息将准确返回您在页面中添加短代码的位置。
For example, if you have an image, then shortcode, then text:
Echo: will output above the image
Return: will output after the image and before the text (where you actually added the shortcode)
例如,如果您有一个图像,然后是简码,然后是文本:
Echo:将在图像上方输出
Return:将在图像之后和文本之前输出(您实际添加短代码的位置)
回答by Anonymous
The difference is that echosends the text directly to the page without the function needing to end. returnboth ends the function and sends the text back to the function call.
不同的是echo直接将文本发送到页面,函数不需要结束。 return两者都结束函数并将文本发送回函数调用。
For echo:
对于回声:
function foobar_shortcode($atts) {
echo "Foo"; // "Foo" is echoed to the page
echo "Bar"; // "Bar" is echoed to the page
}
$var = foobar_shortcode() // $var has a value of NULL
For return:
退货:
function foobar_shortcode($atts) {
return "Foo"; // "Foo" is returned, terminating the function
echo "Bar"; // This line is never reached
}
$var = foobar_shortcode() // $var has a value of "Foo"
回答by HashHazard
Its not that echo and return are the same thing.. it's just that once the echo completes in your first function there is nothing left to do... so it returns..
它并不是说 echo 和 return 是一回事。只是一旦 echo 在你的第一个函数中完成,就没有什么可做的了......所以它返回..
In the second fx your are explicitly exiting the function and returning the value back to the calling function.
在第二个 fx 中,您显式退出函数并将值返回给调用函数。
回答by Bas Kuis
I would use:
我会用:
function foobar_shortcode($atts) {
return "Foo Bar"; //so does this
}
It is easier when you're doing things like:
当您执行以下操作时会更容易:
$output = '<div class="container">' . do_shortcode('foobar') . '</div>';
echo $ouput;
Later on..
稍后的..

