WordPress页面内容随机插入短代码
WordPress page content randomly inserting inside of shortcode
我正在编写一个插件,您可以在页面中输入简码,它会显示 table 数据。
但是,短代码之前的页面内容被随机插入 table 的中间。当我刷新页面时,在短代码上方键入的内容随机移动到由短代码生成的 table 中。
短代码下的内容不会出现在短代码中 return。
有谁知道为什么会发生这种情况。这太奇怪了。
----------------wordpress页面编辑----------------
这里是一些内容。
这是另一段。
[view_contributions]
页面内容结束。
----------------wordpress页面编辑结束-------------------- ------
然后生成
------显示------
[简码数据 table "Here is some content. Here is another paragraph." 随机插入某个单元格中。然后更多数据table]
页面内容结束。
--------显示结束-----
这太奇怪了。就好像短代码首先呈现,然后 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"> </textarea></td>
<td><select> </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> </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;
}
您似乎正试图 return 将输出缓冲区作为字符串。当调用 ob_start() 时,所有输出都被抑制,直到 ob_end()、ob_end_flush() 或 ob_end_clean() 被如此逻辑地调用,你不能return它。只需在函数本身和 return 中调用它们 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 在尝试 return 从一个函数的整个缓冲区时不抛出致命错误让我感到惊讶。
问题原来是在输出缓冲区中混合了 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> </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> </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>';
我正在编写一个插件,您可以在页面中输入简码,它会显示 table 数据。
但是,短代码之前的页面内容被随机插入 table 的中间。当我刷新页面时,在短代码上方键入的内容随机移动到由短代码生成的 table 中。
短代码下的内容不会出现在短代码中 return。
有谁知道为什么会发生这种情况。这太奇怪了。
----------------wordpress页面编辑----------------
这里是一些内容。
这是另一段。
[view_contributions]
页面内容结束。
----------------wordpress页面编辑结束-------------------- ------
然后生成
------显示------
[简码数据 table "Here is some content. Here is another paragraph." 随机插入某个单元格中。然后更多数据table]
页面内容结束。
--------显示结束-----
这太奇怪了。就好像短代码首先呈现,然后 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"> </textarea></td>
<td><select> </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> </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;
}
您似乎正试图 return 将输出缓冲区作为字符串。当调用 ob_start() 时,所有输出都被抑制,直到 ob_end()、ob_end_flush() 或 ob_end_clean() 被如此逻辑地调用,你不能return它。只需在函数本身和 return 中调用它们 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 在尝试 return 从一个函数的整个缓冲区时不抛出致命错误让我感到惊讶。
问题原来是在输出缓冲区中混合了 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> </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> </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>';