問題描述

我遇到這個錯誤。我不知道處理這個。

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 將需要的用户重定向到其自身邏輯的一部分。

次佳解決辦法

如果您嘗試從當前頁面重定向到另一個頁面,那麼您已經處理了條件或沒有條件,那麼請使用此代碼。例如,你有兩個頁面 A.php,& B.php 和當前你在 A.php 你想去其他頁面 B.php 點擊按鈕。

   if(isset($_POST['save_btn']))
    {
        //write some of your code here, if necessary
        echo'<script> window.location="B.php"; </script> ';
     }

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。