php - 无法修改 header 信息 - header 已由...发送 WordPress 问题

标签 php wordpress

<分区>

我遇到了这个错误。我不知道如何处理这个问题。

Cannot modify header information - headers already sent by (output started at /home/ben213/public_html/wp-content/themes/Bendaggers/functions.php:9) in /home/ben213/public_html/wp-includes/pluggable.php on line 934

我的 Functions.php 文件第 9 行是:

<?php if(function_exists('register_sidebar'))register_sidebar();?>

而我的 pluggable.php #934 是

function wp_redirect($location, $status = 302) {
    global $is_IIS;

    $location = apply_filters('wp_redirect', $location, $status);
    $status = apply_filters('wp_redirect_status', $status, $location);

    if ( !$location ) // allows the wp_redirect filter to cancel a redirect
        return false;

    $location = wp_sanitize_redirect($location);

    if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
        status_header($status); // This causes problems on IIS and some FastCGI setups

    header("Location: $location", true, $status);}
endif;

我很难弄清楚这一点,因为我不是程序员。似乎出了什么问题? 请帮助我...

最佳答案

您的主题是将输出(文本)打印到浏览器,但由于某种原因,WordPress 在整个页面呈现之前将用户(使用 wp_redirect)重定向到该页面之外。您不能开始打印输出然后重定向,否则您将收到您看到的错误。这就是 Paul Grime 在他的评论中所要表达的。

Ken White 评论了一篇有类似问题的帖子。根据我自己的经验,我通过缓冲脚本的输出解决了这个问题。

在主题的 functions.php 文件(每次加载主题页面时都会包含该文件)中,输入以下内容:

//allow redirection, even if my theme starts to send output to the browser
add_action('init', 'do_output_buffer');
function do_output_buffer() {
        ob_start();
}

现在,即使您的部分主题开始向浏览器发送输入,PHP 在页面完全加载之前不会发送该文本,这允许 WordPress 在必要时重定向用户,作为其自身逻辑的一部分。

关于php - 无法修改 header 信息 - header 已由...发送 WordPress 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7381661/

相关文章:

php - DataTables 分页和搜索不起作用

php - WordPress - 列出类别限制

php - 在新订单管理 Woocommerce 电子邮件中添加自定义数据的钩子(Hook)

jquery - 尝试将 flexslider 控件移动到 slider 窗口的中间

wordpress - 插件激活后重定向

php - 如何测试数组中的部分字符串值

php - CodeIgniter 加入无法选择

php - 数据未插入第二个表 (MYSQLi)

php - 我如何在golang中匹配phpseclib1 Rijndael.php CBC AES加密?

php - Facebook 个人资料图像并将其下载到我的网络目录