【问题标题】:Wordpress using echo vs return in shortcode functionWordpress 在简码函数中使用 echo vs return
【发布时间】:2014-02-27 13:52:06
【问题描述】:

我注意到echoreturn 都可以很好地在 wordpress 中显示来自简码函数的内容。

function foobar_shortcode($atts) {
    echo "Foo Bar"; //this works fine
}

function foobar_shortcode($atts) {
    return "Foo Bar"; //so does this
}

使用这两种方法有什么区别吗?如果是,wordpress 的推荐方法是什么?在这种情况下,我通常使用echo - 可以吗?

【问题讨论】:

  • 一个立即输出,另一个将文本返回给调用函数,由调用函数处理输出。它们不一样,功能也不相同。

标签: php wordpress shortcode


【解决方案1】:

如果你输出的内容很多,那么你应该使用:

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();
}

【讨论】:

  • 太棒了!这正是我所需要的。 PHP 中的输出缓冲 (ob_start) 是我不知道的。在这里可以找到一些有助于解释此答案的有用信息:stackoverflow.com/questions/2832010/what-is-output-buffering
  • 好吧,它有效!现在我可以将我的简码内容准确地输出到我期望的位置。
【解决方案2】:

Echo 可能适用于您的特定情况,但您绝对不应该使用它。简码并不意味着输出任何东西,它们应该只返回内容。

以下是简码法典中的注释:

请注意,短代码调用的函数不应该产生 任何形式的输出。简码函数应该返回的文本是 用于替换简码。直接生成输出 会导致意想不到的结果。

http://codex.wordpress.org/Function_Reference/add_shortcode#Notes

输出缓冲

有时您会遇到输出变得难以避免或难以避免的情况。例如,您可能需要调用一个函数来在您的短代码回调中生成一些标记。如果该函数直接输出而不是返回值,您可以使用一种称为输出缓冲的技术来处理它。

输出缓冲将允许您捕获代码生成的任何输出并将其复制到字符串中。

使用ob_start() 开始一个缓冲区,并确保在完成时抓取内容并删除它,ob_get_clean()。出现在两个函数之间的任何输出都将写入内部缓冲区。

例子:

function foobar_shortcode( $atts ) {
    ob_start();

    // any output after ob_start() will be stored in an internal buffer...
    example_function_that_generates_output();

    // example from original question - use of echo
    echo 'Foo Bar';

    // we can even close / reopen our PHP tags to directly insert HTML.
    ?>
        <p>Hello World</p>
    <?php

    // return the buffer contents and delete
    return ob_get_clean();
}
add_shortcode( 'foobar', 'foobar_shortcode' );

https://www.php.net/manual/en/function.ob-start.php

【讨论】:

  • 谢谢!这就是我想知道的
【解决方案3】:

如果您在短代码中使用“echo”,则信息将显示在处理短代码的任何位置,这不一定是您实际添加短代码的位置。如果您使用“return”,则信息将准确返回您在页面中添加短代码的位置。

例如,如果您有一张图片,然后是短代码,然后是文字:
Echo:将在图像上方输出
返回:将在图像之后和文本之前输出(您实际添加短代码的位置)

【讨论】:

  • 这个例子很好地说明了这一点。谢谢
【解决方案4】:

我会使用:

function foobar_shortcode($atts) {
    return "Foo Bar"; //so does this
}

当您执行以下操作时会更容易:

$output = '<div class="container">' . do_shortcode('foobar') . '</div>';
echo $ouput;

稍后……

【讨论】:

    【解决方案5】:

    不同之处在于echo 将文本直接发送到页面而不需要结束函数。 return 结束函数并将文本发送回函数调用。

    对于回声:

    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
    

    退货:

    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"
    

    【讨论】:

      【解决方案6】:

      这不是 echo 和 return 是一回事。只是一旦 echo 在您的第一个函数中完成,就没有什么可做的了...所以它返回了..

      在第二个 fx 中,您明确退出函数并将值返回给调用函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-23
        • 2014-03-15
        • 2012-12-07
        • 2019-07-05
        • 1970-01-01
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多