无法修改标题信息 - 已由...发送的标题Wordpress问题

Ben*_*ers 40 php wordpress

我遇到了这个错误.我不知道处理这件事.

无法修改标题信息 - 已在/home/ben213/public_html/wp-includes/pluggable.php中发送的标题(由/home/ben213/public_html/wp-content/themes/Bendaggers/functions.php:9开始输出) 934行

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

<?php if(function_exists('register_sidebar'))register_sidebar();?>
Run Code Online (Sandbox Code Playgroud)

而我的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;
Run Code Online (Sandbox Code Playgroud)

因为我不是程序员,所以我很难搞清楚这一点.什么似乎是错的?请帮助我...

小智 82

您的主题是将输出(文本)打印到浏览器,但由于某种原因,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();
}
Run Code Online (Sandbox Code Playgroud)

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

  • 这有副作用吗? (12认同)
  • 是的,这有副作用,内存使用是最明显的,较慢的站点响应,因为整个页面必须在发送任何内容之前呈现. (5认同)

Pir*_*hah 19

如果您尝试从当前页面重定向到另一个页面,您已施加条件或没有条件,则使用此代码.E.gs你有两页A.php,和B.php,你当时在A.php,你想要点击BUTTON去其他页面B.php.

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