‘wp_redirect’ is not working

There is an HTML form input. Here’s the code:

<?php if(isset($_POST['login'])) { 
    wp_redirect("/"); 
}

<form accept-charset="UTF-8" method="post" >
    ...
    <center><input name="login" type="submit" value="вход" />
</form>

But redirect doesn’t work. Install debug plugin redirects to wp, that’s what it showed.

Read More

http://i.stack.imgur.com/Im4eE.png

PS:

<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

It does not work either.

Related posts

Leave a Reply

6 comments

  1. I think your code doesn’t begin with that if condition!

    wp_redirect will send an header, so printing/echoing something before it, will have result in failure.

    So check and see if before this:

    if(isset($_POST['login'])) 
    {
        wp_redirect("/");
        exit;
    }
    

    there is no character out put. Also do not forget to put exit; right after wp_redirect.

  2. Just use this as per below:

    ob_clean();
    $url = get_home_url() . '/login';
    wp_redirect($url);
    exit();
    

    Or you could use Javascript as well for redirection purpose.

    <script>window.location='http://www.google.com'</script>
    
  3. Make sure you don’t have: get_header(); or any wordpress function that potentially creates contents like header and footer in your template. Otherwise the redirection won’t work.

    Some developers try to clear the page by using ob_start(); but if you have content in your page even if you use ob_start(); the redirection won’t work.

    and then simply try this code:

    wp_redirect(get_permalink($post->ID));
    exit;
    
  4. Try the following, which also forces on error reporting:

    error_reporting(E_ALL | E_WARNING | E_NOTICE);
    ini_set('display_errors', TRUE);
    
    
    flush();
    header("Location: http://www.website.com/");
    die('should have redirected by now');
    

    From PHP header redirect not working

    Edit:

    Since it’s giving you the headers already sent warning, try adding the following at the very beginning of your code:

    ob_start();

    The long term answer is that all output from your PHP scripts should
    be buffered in variables. This includes headers and body output. Then
    at the end of your scripts do any output you need.

    The very quick fix for your problem will be to add ob_start(); as the
    very first thing in your script if you only need it in this one
    script. If you need it in all your scripts add it as the very first
    thing in your header.php file.

    This turns on PHP’s output buffering feature. In PHP when you output
    something (do an echo or print) if has to send the HTTP headers at
    that time. If you turn on output buffering you can output in the
    script but PHP doesn’t have to send the headers until the buffer is
    flushed. If you turn it on and don’t turn it off PHP will
    automatically flush everything in the buffer after the script finishes
    running. There really is no harm in just turning it on in almost all
    cases and could give you a small performance increase under some
    configurations…

    From Warning: Cannot modify header information – headers already sent..

  5. I faced same problem and none of these solutions worked for me.

    The only thing that I noticed is different at the page is that I used wp_redirect below get_header() and it will work fine if you used it above it.