'wp_redirect' does not work

There is an input HTML form. Here is 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 the redirection does not work. Install the debug plugin to redirect to wp, which it showed.

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

PS:

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

It doesn't work either.

+8
source share
6 answers

Use the following code: -

 function app_output_buffer() { ob_start(); } // soi_output_buffer add_action('init', 'app_output_buffer'); 

Or add ob_start () as the first line of your own function, which connects to 'init'

Do not forget to add

 exit(); 

right after your call

 wp_redirect($url); 

link:

https://tommcfarlin.com/wp_redirect-headers-already-sent/

+7
source

I think your code does not start with this if !

wp_redirect will send the header, so printing / echoing in front of it will fail.

So check and see if before:

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

no character. Also do not forget to put exit; immediately after wp_redirect .

+6
source

Just use this as shown below:

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

Or you can use Javascript as well for redirection purposes.

 <script>window.location='http://www.google.com'</script> 
+1
source

Try the following, which also affects the error report:

 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'); 

PHP header redirection not working

Edit:

Since it gives you a warning headers already sent , try adding the following to the 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 headings and body output. then at the end of your scripts you need any result.

The quickest solution is to add ob_start (); as very important in your script if you need only in this script. If you need it in all of your scripts, add it as the first in your header.php file.

This includes the PHP output buffering function. In PHP, output something (do an echo or print) if you need to send HTTP headers at that time. If you enable output buffering, you can output to the script, but PHP should not send headers until the buffer is flushed. If you enable it and don’t disable it, PHP will automatically clear everything in the buffer after the script runs. Actually there is no harm just turning it on in almost all cases and may give you a slight increase in performance with some configurations ...

From Warning: It is not possible to change header information - headers already sent ..

0
source

I ran into the same problem and none of these solutions helped me.

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

0
source

Make sure you do not have: get_header(); or any WordPress feature that potentially creates content like the header and footer in your template. Otherwise, the redirect will not work.

Some developers try to clear the page using ob_start(); but if you have content on your page, even if you use ob_start(); redirection will not work.

and then just try this code:

 wp_redirect(get_permalink($post->ID)); exit; 
0
source

Source: https://habr.com/ru/post/956705/


All Articles