最近更新主题的时候,使用了 wp_redirect() 函数来做一个页面跳转,没想到出现了“Warning: Cannot modify header information – headers already sent”的错误提示:

搜索了下,php中使用重定向跳转时,“Warning: Cannot modify header information – headers already sent by (output started at…”是常见的错误提醒,出现该错误的原因是跳转函数前有包括回车、空格、换行的输出,解决方法是使用ob_start()函数打开缓冲区,使用跳转前的输入进入缓冲区而不会立即输出,避免这个错误提醒。
如果是 WordPress 使用 wp_redirect() 函数导致的这个问题,在当前主题的 functions.php 文件中添加以下函数即可:
/**
* 解决 wp_redirect()导致的 Warning: Cannot modify header information 问题
* https://www.wpdaxue.com/wp_redirect-Cannot-modify-header-information.html
*/
function cmp_do_output_buffer() {
ob_start();
}
add_action('init', 'cmp_do_output_buffer'); |
/**
* 解决 wp_redirect()导致的 Warning: Cannot modify header information 问题
* https://www.wpdaxue.com/wp_redirect-Cannot-modify-header-information.html
*/
function cmp_do_output_buffer() {
ob_start();
}
add_action('init', 'cmp_do_output_buffer');
如果大家还有其他解决办法,欢迎和我们一起分享。
参考资料:
http://www.boke8.net/wordpress-cannot-modify-header-information.html
http://stackoverflow.com/questions/7381661/cannot-modify-header-information-headers-already-sent-by-wordpress-issue#answer-7461813
来源:
https://www.wpdaxue.com/wp_redirect-cannot-modify-header-information.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33684.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57