wp_redirect

Redirects to another page.

Description

wp_redirect( (string) $location, (int) $status = 302 ); 

Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;:

Exiting can also be selectively manipulated by using wp_redirect() as a conditional in conjunction with the and hooks:

exit;

Parameters (2)

0. $location (string)
The path to redirect to.
1. $status — Optional. (int) => 302
Status code to use.

Usage

  1. if ( !function_exists( 'wp_redirect' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // The path to redirect to. 
  5. $location = ''; 
  6.  
  7. // Status code to use. 
  8. $status = 302; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_redirect($location, $status); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/pluggable.php  
  1. function wp_redirect($location, $status = 302) { 
  2. global $is_IIS; 
  3.  
  4. /** 
  5. * Filters the redirect location. 
  6. * @since 2.1.0 
  7. * @param string $location The path to redirect to. 
  8. * @param int $status Status code to use. 
  9. */ 
  10. $location = apply_filters( 'wp_redirect', $location, $status ); 
  11.  
  12. /** 
  13. * Filters the redirect status code. 
  14. * @since 2.3.0 
  15. * @param int $status Status code to use. 
  16. * @param string $location The path to redirect to. 
  17. */ 
  18. $status = apply_filters( 'wp_redirect_status', $status, $location ); 
  19.  
  20. if ( ! $location ) 
  21. return false; 
  22.  
  23. $location = wp_sanitize_redirect($location); 
  24.  
  25. if ( !$is_IIS && PHP_SAPI != 'cgi-fcgi' ) 
  26. status_header($status); // This causes problems on IIS and some FastCGI setups 
  27.  
  28. header("Location: $location", true, $status); 
  29.  
  30. return true;