Title: Passing function into wp_safe_redirect
Last modified: November 12, 2022

---

# Passing function into wp_safe_redirect

 *  Resolved [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/)
 * I have a function that returns a value like:
 *     ```
       function test () {
          return 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
       }
       ```
   
 * And it doesn’t work if I do it with `wp_safe_redirect` like this:
    `wp_safe_redirect(
   test () );` I understand this is not valid in PHP, so I’ve also tried:
 *     ```
       $temporary = test ();
   
       wp_safe_redirect ( $temporary );
       ```
   
 * But it’s not working too…

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16192368)
 * Maybe you neglected to execute `exit;` after wp_safe_redirect()? [It’s essential](https://developer.wordpress.org/reference/functions/wp_safe_redirect/#description).
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16192859)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) I actually have the `
   exit` after the `wp_safe_redirect ()`, but it’s not working…
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16193091)
 * [@bcworkz](https://wordpress.org/support/users/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?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16193755)
 * 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.
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16193764)
 * [@bcworkz](https://wordpress.org/support/users/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.
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16195142)
 * [@bcworkz](https://wordpress.org/support/users/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`
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16196049)
 * [@bcworkz](https://wordpress.org/support/users/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.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16197263)
 * 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.
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16198591)
 * [@bcworkz](https://wordpress.org/support/users/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](https://developer.wordpress.org/reference/hooks/admin_post_action/).
   So the redirect code is not on the page itself…
 * Thank you so much for your advice!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16201159)
 * 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.
 *  Thread Starter [ace0930](https://wordpress.org/support/users/ace0930/)
 * (@ace0930)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16202949)
 * [@bcworkz](https://wordpress.org/support/users/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](https://developer.wordpress.org/reference/hooks/admin_post_action/).
   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.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Passing function into wp_safe_redirect’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 11 replies
 * 2 participants
 * Last reply from: [ace0930](https://wordpress.org/support/users/ace0930/)
 * Last activity: [3 years, 7 months ago](https://wordpress.org/support/topic/passing-function-into-wp_safe_redirect/#post-16202949)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
