WP_redirect not working (header already sent in pluggable.php)

I want to use this code in a template file:

if ( wp_is_mobile() ) {
wp_redirect( "/shop-mobile", $status );
}

but it says: header already sent by /filewithcode in …/wp_includes/pluggable.php on line 1196

Read More

I tried to clean the pluggable document from spaces.. what else could be the issue here? Thank you

Related posts

2 comments

  1. “To fix the “headers already sent” issue, you need to move all of your form processing from the bottom of the page to the top of the page. If you need to call wp_redirect() you must make that call before you print anything – HTML or anything else – to the page.”

    reference:
    https://wordpress.stackexchange.com/questions/81566/wp-redirect-headers-already-sent-after-front-end-submission-form

    you can read this for more explanation of this problem

    How to fix “Headers already sent” error in PHP

  2. Ensure that the code above the wp_redirect function have not already sent the header information to the server.

    Header information will be sent to the server in some of the following scenarios:

    1. print, echo

    2. Space before <?php or space after ?>

    Check if the headers are being already sent using:

    if (headers_sent()) {
         die("Redirect failed. Please click on this link: <a href=...>");
    }
    else{
         exit(header("Location:/test.php"));
    }
    

    Refer the below link for further guidance.How to fix “Headers already sent” error in PHP

Comments are closed.