帮助中心 >  技术知识库 >  虚拟主机 >  虚拟主机常见问题及技术支持 >  WordPress 使用 wp_redirect 函数出现 Warning: Cannot modify header information – headers already sent

WordPress 使用 wp_redirect 函数出现 Warning: Cannot modify header information – headers already sent

2017-02-27 23:11:13 14792

WordPress 使用 wp_redirect 函数出现 Warning: Cannot modify header information – headers already sent


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

2015-10-29_193459_wpdaxue_com

搜索了下,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.landui.com/wp_redirect-Cannot-modify-header-information.html
*/function cmp_do_output_buffer() {
    ob_start();}add_action('init', 'cmp_do_output_buffer');


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: