I am inserting a piece of code to my website that includes a redirect statement. Everything works fine except the wp_redirect();
. The following warning message appears.
Cannot modify header information - headers already sent by (output started at /home/xyz.com/b14_32999707/htdocs/wp-includes/class-wp-styles.php:350) in /home/xyz.com/b14_32999707/htdocs/wp-includes/pluggable.php on line 1416
Following is line 1416 of the mentioned file.
$x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location );
if ( is_string( $x_redirect_by ) ) {
header( "X-Redirect-By: $x_redirect_by" ); //Line 1416
}
header( "Location: $location", true, $status );
return true;
Tried each and every solution available on the web but nothing works except the JavaScript redirect which I don't want to use. Even a very simple php code like the following doesn't work.
<?php
if(...){
wp_redirect('http://www.my-site.com/my-page/');
exit();
}
wp_redirect
? You can't redirect if the server has sent output to the browser, even a single space character will send the HTTP headers, and HTTP headers can only be sent once. Once they are sent it is too late. This is one of the reasons it is impossible to usewp_redirect
inside blocks widgets and shortcodes, because it is too late. You can either output, or you can redirect, once you begin output, redirection becomes impossible.