简体   繁体   English

WP_redirect无法正常工作(标头已在pluggable.php中发送)

[英]WP_redirect not working (header already sent in pluggable.php)

I want to use this code in a template file: 我想在模板文件中使用此代码:

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

but it says: header already sent by /filewithcode in .../wp_includes/pluggable.php on line 1196 但它说:标头已经由/ filewithcode在... / wp_includes / pluggable.php中的第1196行发送

I tried to clean the pluggable document from spaces.. what else could be the issue here? 我试图从空格中清除可插拔文档。.还有什么可能是这里的问题? Thank you 谢谢

"To fix the "headers already sent" issue, you need to move all of your form processing from the bottom of the page to the top of the page. If you need to call wp_redirect() you must make that call before you print anything - HTML or anything else - to the page." “要解决“已发送的标题”问题,您需要将所有表单处理从页面底部移至页面顶部。如果需要调用wp_redirect(),则必须先进行调用,然后再打印任何内容-HTML或其他内容-页面。”

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

you can read this for more explanation of this problem 您可以阅读以获取有关此问题的更多解释

How to fix "Headers already sent" error in PHP 如何修复PHP中的“标头已发送”错误

Ensure that the code above the wp_redirect function have not already sent the header information to the server. 确保wp_redirect函数上方的代码尚未将标头信息发送到服务器。

Header information will be sent to the server in some of the following scenarios: 标头信息将在以下某些情况下发送到服务器:

  1. print, echo 打印,回显

  2. Space before <?php or space after ?> <?php or space after ?>之前的<?php or space after ?>

Check if the headers are being already sent using: 使用以下命令检查标题是否已发送:

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

Refer the below link for further guidance. 请参考以下链接以获取更多指导。 How to fix "Headers already sent" error in PHP 如何修复PHP中的“标头已发送”错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM