簡體   English   中英

WordPress頁面內容隨機插入簡碼內

[英]WordPress page content randomly inserting inside of shortcode

我正在編寫一個插件,您可以在其中在頁面中輸入一個簡碼,並顯示一個數據表。

但是,短代碼之前的頁面內容將隨機插入表的中間。 當我刷新頁面時,在簡碼上方鍵入的內容會在簡碼生成的表中隨機移動。

簡碼下的內容不會出現在簡碼返回中。

有誰知道為什么會這樣。 這真是太奇怪了。

------------------ 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_start()時,所有輸出都會被抑制,直到從邏輯上調用ob_end(),ob_end_flush()或ob_end_clean()為止,您無法將其返回。 只需在函數本身中調用它們,然后以字符串形式返回輸出緩沖區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不會引發致命錯誤,這令我感到驚訝。

問題出在輸出緩沖區中混用了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>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM