【问题标题】:WP_redirect not working (header already sent in pluggable.php)WP_redirect 不起作用(标头已在 pluggable.php 中发送)
【发布时间】:2015-09-30 17:06:23
【问题描述】:

我想在模板文件中使用这段代码:

if ( wp_is_mobile() ) {
wp_redirect( "/shop-mobile", $status );
}

但它说:/filewithcode 在第 1196 行的 .../wp_includes/pluggable.php 中已发送标头

我试图从空格中清除可插入文档。这里还有什么问题?谢谢

【问题讨论】:

  • 我知道 6 年前有人问过这个问题 - 但只是偶然发现了这个问题。您可能应该使用响应式网页设计而不是 /shop-mobile

标签: php wordpress redirect


【解决方案1】:

确保 wp_redirect 函数上面的代码还没有将 header 信息发送到服务器。

在以下一些场景中会向服务器发送header信息:

  1. 打印,回显

  2. <?php or space after ?>之前的空格

检查是否已发送标头:

if (headers_sent()) {
     die("Redirect failed. Please click on this link: <a href=...>");
}
else{
     exit(header("Location:/test.php"));
}

请参阅以下链接以获取更多指导。How to fix "Headers already sent" error in PHP

【讨论】:

    【解决方案2】:

    “要解决“标头已发送”的问题,您需要将所有表单处理从页面底部移动到页面顶部。如果您需要调用 wp_redirect(),则必须先调用您可以将任何内容(HTML 或其他内容)打印到页面上。”

    参考: https://wordpress.stackexchange.com/questions/81566/wp-redirect-headers-already-sent-after-front-end-submission-form

    您可以阅读本文以了解有关此问题的更多说明

    How to fix "Headers already sent" error in PHP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 2016-06-17
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多