3

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();
}
4
  • 2
    where are you calling 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 use wp_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.
    – Tom J Nowell
    Jan 31, 2023 at 17:05
  • Yeah I am inserting this code using a plugin through shortcode. There are some more contents created through Elementor and at the end this shortcode.
    – Asmat Ali
    Jan 31, 2023 at 17:09
  • I have set a local server using Wamp and created a simple wordpress website where I test various stuff for learning purposes. I inserted this code there, and it works, but not on the online website I am managing.
    – Asmat Ali
    Jan 31, 2023 at 17:13
  • you can't do redirects in a shortcode. It's possible you have some sort of output buffering that prevented output from happening that is happening locally but not on production, but if you want to do a redirect on an event, a shortcode/widget is the worst place to put it, it's just too late in the page load
    – Tom J Nowell
    Jan 31, 2023 at 17:25

1 Answer 1

6

The problem is not what you are doing, but when you are doing it.

When your browser requests a webpage, that webpage arrives with a set of HTTP headers, followed by a HTTP body that contains the HTML. Redirection is done via a HTTP header. The moment you echo, or printf or send any form of HTML, or even blank space PHP will send HTTP headers telling the browser to expect a webpage, then it will start sending the HTTP body. When this happens your opportunity to redirect has gone, and any attempt to send a HTTP header will give you the error in your question.

For this reason, you cannot trigger http redirects in shortcodes, widgets, templates, etc.

Instead, do it earlier, before output begins, preferably in a hook, e.g.

add_action( 'init', 'asmat_maybe_redirect' );
function asmat_maybe_redirect() : void {
    if ( ... ) {
        wp_redirect( 'http://www.my-site.com/my-page/' );
        exit();
    }
}

Additionally, if that redirect is to somewhere else on the same site, use wp_safe_redirect instead. But whatever you do, it must happen early before any output occurs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.