Maybe you neglected to execute exit; after wp_safe_redirect()? It’s essential.
@bcworkz I actually have the exit after the wp_safe_redirect (), but it’s not working…
@bcworkz I have realized that it’s caused by the $_SERVER['REQUEST_URI'] part, as soon as it’s removed, it works.
Why and how $_SERVER['REQUEST_URI'] causes wp_safe_redirect () not to work? Is this a bug?
I’m not sure. What’s the purpose of redirecting to the same place? Doesn’t that cause a too many redirect’s error? I think there’d need to be a mechanism to only let it happen once.
@bcworkz I have created a menu page with a form, I want the user to go back to the menu page ( same place ) after submitting the form.
Sorry because I don’t understand how $_SERVER['REQUEST_URI'] will cause so many errors, can you elaborate more?
Or actually, do you have a better alternative for this? My ideal result is sending the user back to the place where they submit the form. This cannot be hardcoded and it should be dynamic.
@bcworkz I have also tried:
$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
It redirects to: https://example.com/wp-admin/admin-post.php?.
With the original code:
https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
It always redirects to https://example.com/wp-admin/admin-post.php
@bcworkz Okay, cannot believe this mistake took me two days, I think I know why.
Because the action of the form is set to admin-post.php, of course, the current URL would always be that…LOL
But you may still explain why $_SERVER['REQUEST_URI'] may cause so many redirect error.
I’m not sure how to better explain. If you put a redirect statement on a code page that redirects to the same page, the code will constantly loop around itself ad infinitum unless there’s additional code to break the loop.
Also note that PHP redirects need to occur before any output occurs because it is accomplished by sending a Location: header. All headers must be sent prior to any output, or we get headers already sent errors. Maybe this is where things are going wrong for you?
There are a few ways to handle form submits. Redirecting back to the same page isn’t one of them 🙂 Look at how the default pop-up admin login form works. The current URL is passed as a query string to wp-logon.php (redirect_to query string). The form data is handled (no output), then code redirects to where ever the query string indicated.
You could instead handle form submits via Ajax, thus the user never leaves the current page. Or have the form submit to the very same page (empty action attribute) using POST method. The page’s code can then distinguish between GET (empty form) and POST (there’s data to process) requests and behave accordingly.
@bcworkz Nope, it seems you misunderstood my point. It’s my fault not to mention that the redirect code is in a function that will be triggered after the form is submitted using this method. So the redirect code is not on the page itself…
Thank you so much for your advice!
REQUEST_URI is the last request. So by whatever path you use to get to using it, you’re redirecting back to the same place. So unless there’s a conditional to intercept that process and go elsewhere, execution will infinitely loop around.
Anyway, IMO there are better ways to return a user to where they were than redirecting to REQUEST_URI.
@bcworkz Nonono, you didn’t get the point. I didn’t put the wp_safe_redirect on the first line of the page like:
<?php wp_safe_redirect ( $example ); ?>
<form></form>
Instead, it’s in a function that would be called once the form is submitted to the admin-post.php with this hook. And it looks like this:
<?php function send () { wp_safe_redirect ( $example ); } ?>
<form></form>
Which mean the wp_safe_redirect ( $example ) will not trigger until the function is called.