【问题标题】:WordPress page content randomly inserting inside of shortcodeWordPress页面内容随机插入短代码内
【发布时间】:2019-03-23 14:07:47
【问题描述】:

我正在编写一个插件,您可以在其中在页面中输入一个短代码,它会显示一个数据表。

但是,短代码之前的页面内容被随机插入到表格的中间。当我刷新页面时,在简码上方键入的内容会在简码生成的表格中随机移动。

短代码下的内容不会出现在短代码返回中。

有谁知道为什么会发生这种情况。这太奇怪了。

------------------ wordpress 页面编辑---------------

这里有一些内容。

这是另一段。

[view_contributions]

页面内容结束。

--------------------wordpress 页面编辑结束------------------------ ----

然后产生

-----显示--------

[短代码数据表带有“这是一些内容。这是另一个段落。”随机插入某处的单元格中。然后更多数据表]

页面内容结束。

-------- 显示结束 -----

这太奇怪了。就好像简码首先呈现,然后 WordPress 将页面内容注入到简码正在呈现的任何内容中。有什么想法可能导致这种情况吗?

编辑:添加完整的代码以防发生真正奇怪的事情......

function soco_view_contributions_shortcode() { 
$view_contributions = Soco_Contributions::soco_display_contributions();
return $view_contributions;
}
add_shortcode( 'view_contributions', 'soco_view_contributions_shortcode');


    public function soco_display_contributions() {
    $contribution_results = Soco_Contributions::soco_get_contributions_view();

    ob_start;
?>      
<div name="div-output-container"> 

    <form name="frm-search-contributions">
        <table width="100%" border="0">
          <tbody>
            <tr>
              <th scope="col">Start Date</th>
              <th scope="col">End Date</th>
              <th scope="col">Minimum</th>
              <th scope="col">Maximum</th>
              <th scope="col">Name</th>
              <th scope="col">Event</th>
            </tr>
            <tr>
              <td><input type="date" name="start-date"></td>
              <td><input name="end-date" type="date" ></td>
              <td><input type="number" name="low-number"></td>
              <td><input type="number" name="high-number"></td>
              <td><text name="txt-auto-name">&nbsp;</textarea></td>
              <td><select>&nbsp;</select></td>
            </tr>
          </tbody>
        </table>
        <input type="submit">
        <input type="reset">
    </form>

    <table width="100%" border="0">
      <tbody>
        <tr>
          <th scope="col">Date</th>
          <th scope="col">Amount</th>
          <th scope="col">Cycle</th>
          <th scope="col">Name</th>
          <th scope="col">Event</th>
        </tr>

<?php   foreach ($contribution_results as $cr) {  ?>
            <tr>
              <td><?php echo $cr->contribution_date ?></td>
              <td><?php echo $cr->amount ?></td>
              <td><?php echo $cr->cycle_amount ?></td>
              <td><?php echo $cr->last_name.', '.$cr->first_name ?></td>
              <td>&nbsp;</td>
            </tr>

<?php   }  ?>

        </tbody>
    </table>

    <button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>

</div>
<?php   
        $contribution_output = ob_get_clean();

        return $contribution_output;
    }

【问题讨论】:

  • 你在哪里刷新输出缓冲?
  • 我需要在某处使用 ob_flush 或 flush 吗?我看到的例子只是在开头使用了ob_start。然后 ob_get_clean 最后没有其他任何东西。我过去曾使用过它,并且效果很好。它现在不是那样工作的。如果我需要在代码中做其他事情,请告诉我。感谢您查看此内容!
  • 输出缓冲可能是这种情况下的因素。我将添加一个可能对您的案例有所帮助的答案。

标签: php wordpress render shortcode


【解决方案1】:

问题原来是在输出缓冲区中混合了 ECHO 和 RETURN。为了解决这个问题,我把整个事情变成了一个串联的字符串来输出。

我认为 foreach 循环中的回声弄乱了输出缓冲区。所以我一起删除了 ob_start ,只会输出一个最终的 HTML 字符串。

不是一个很好的解决方案,但至少它现在可以工作并且不会产生随机结果。如果有人有关于如何在其中混合 ob_start 和 php 逻辑的建议或示例,那就太好了。对我来说 ob_start() 有这个问题似乎很奇怪。

$contribution_output = '<div name="div-output-container"> 

    <form name="frm-search-contributions">
        <table width="100%" border="0">
          <tbody>
            <tr>
              <th scope="col">Start Date</th>
              <th scope="col">End Date</th>
              <th scope="col">Minimum</th>
              <th scope="col">Maximum</th>
              <th scope="col">Name</th>
              <th scope="col">Event</th>
            </tr>
            <tr>
              <td><input type="date" name="start-date"></td>
              <td><input name="end-date" type="date" ></td>
              <td><input type="number" name="min-amount"></td>
              <td><input type="number" name="max-amount"></td>
              <td>
                  <input type="text" name="donor_name">
                  <input type="hidden" name="hdn-donor-id" value="">
                </td>
              <td><select>&nbsp;</select></td>
            </tr>
          </tbody>
        </table>
        <input type="submit">
        <input type="reset">
    </form>

    <div name="contribution-results-container" >
        <table width="100%" border="0">
          <tbody>
            <tr>
              <th scope="col">Date</th>
              <th scope="col">Amount</th>
              <th scope="col">Cycle</th>
              <th scope="col">Name</th>
              <th scope="col">Event</th>
            </tr>';

    foreach ($contribution_results as $cr) {  
        $contribution_output .= '
            <tr>
              <td>'.$cr->contribution_date.'</td>
              <td>'.$cr->amount.'</td>
              <td>'.$cr->cycle_amount.'</td>
              <td>'.$cr->last_name.', '.$cr->first_name.'</td>
              <td>&nbsp;</td>
            </tr>';

    }  

    $contribution_output .= '</tbody>
        </table>

        <button name="btnDownload" id="btnDownload" title="Click this button to download the above dataset." >Download CSV File</button>
    </div>
</div>';

【讨论】:

    【解决方案2】:

    您似乎正试图将输出缓冲区作为字符串返回。当 ob_start() 被调用时,所有输出都被抑制,直到 ob_end()、ob_end_flush() 或 ob_end_clean() 被如此逻辑地调用,你不能返回它。只需在函数本身内调用它们并将contents of the output buffer (ob_get_contents()) 作为字符串返回:

    add_shortcode('view_contributions','soco_view_contributions_shortcode');
    function soco_view_contributions_shortcode( ) {
      ob_start();
      ?>
      <h1>Shortcode Output</h1>
      <p><?php echo "Some other output" ?></p>
      <?
      return ob_end_clean();
    } 
    

    为什么 PHP 在尝试从函数返回整个缓冲区时不会抛出致命错误让我感到惊讶。

    【讨论】:

    • 我认为 ob_get_clean() 是 {ob_get_contents() 和 ob_end_clean()} 合二为一。 secure.php.net/manual/en/function.ob-get-clean.php这两者有什么区别?我试过你的方法,但它做同样的事情。还有其他想法吗?
    • 偶尔,根据内容,您可以返回 ob_end_clean() ...我会更新我的答案,以便您试一试...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2016-12-20
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多