Cannot modify header information in wordpress

2019-09-15 01:38发布

问题:

I have a problem with Cannot modify header information in wordpress on Warning: Cannot modify header information - headers already sent by (output started at /home/sixtydev/public_html/voxxydev/wp-content/themes/voxxy/header.php:15) in /home/sixtydev/public_html/voxxydev/wp-includes/pluggable.php on line 865

my redirection code are :

if(isset($_GET['qry'])  && !empty($_GET['qry'])){
    $swl = mysql_query("update wp_share_idea set image".$_GET['qry']."='' where userId='".$delId."'");

    wp_redirect("http://60degreedeveloper.info/voxxydev/user-profile/?msg='".$delId."'");
    exit;

} 

my header.php code:

<?php
/**
 * The Header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="main">
 *
 * @package WordPress
 * @subpackage Voxxy
 * @since Voxxy 1.0
 */
?><html>
<head>
<title>Voxxy</title>
<link rel="profile" href="http://gmpg.org/xfn/11"/>
line no: 15:: <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_url');?>"/>
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>"/>

and plugable.php code:

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
line no:865:-   header("Location: $location", true, $status);
}

I am get confused that where is the problem occured. Thanks in advance

回答1:

wp_redirect will only work if it is the first thing to generate output in Wordpress. It works by setting the http header in the output. Once something else has generated some output, that method no longer works.

In your case, wherever you're calling your redirect code in pluggable.php occurs after get_header() has been called.

You've either got to move this code before your header gets called, or use a javascript function to redirect the user.

You should also NEVER run an update query on your database on an unsanitized $_GET variable.



回答2:

if @cpilko solution does not work (he might be perfectly right) but -

There is a known problem of wordpress regarding the PHP file itself . You need to be sure there are no empty characters / spaces / carriage returns BEFORE the opening <?php tag and none after the closing tag ?>

You will need to check that for ALL your theme files and all plugins .

Regarding the closing tag ?> - you need to ensure it EXISTS (yes, some plugin developers omit that)

Read more here : http://codex.wordpress.org/Answers-Troubleshooting#Headers_already_sent

in 99% of the cases - this is the culprit .



回答3:

What happen if you change bloginfo( 'stylesheet_uri' ) to print get_stylesheet_directory_uri() and bloginfo( 'pingback_url' ) to print get_bloginfo( 'pingback_url' ); ?



回答4:

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

    if ( ! $location )
        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);

    return true;
}
endif;


回答5:

Try ob_start(); on the top in root file wp_config.php