wp_sanitize_redirect

Sanitizes a URL for use in a redirect.

Description

(string) wp_sanitize_redirect( (string) $location ); 

Returns (string)

Redirect-sanitized URL.

Parameters (1)

0. $location (string)
The path to redirect to.

Usage

  1. if ( !function_exists( 'wp_sanitize_redirect' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // The path to redirect to. 
  5. $location = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_sanitize_redirect($location); 
  9.  

Defined (1)

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

/wp-includes/pluggable.php  
  1. function wp_sanitize_redirect($location) { 
  2. $regex = '/ 
  3. (?: [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx 
  4. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 
  5. | [\xE1-\xEC][\x80-\xBF]{2} 
  6. | \xED[\x80-\x9F][\x80-\xBF] 
  7. | [\xEE-\xEF][\x80-\xBF]{2} 
  8. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 
  9. | [\xF1-\xF3][\x80-\xBF]{3} 
  10. | \xF4[\x80-\x8F][\x80-\xBF]{2} 
  11. ) {1, 40} # ...one or more times 
  12. )/x'; 
  13. $location = preg_replace_callback( $regex, '_wp_sanitize_utf8_in_redirect', $location ); 
  14. $location = preg_replace('|[^a-z0-9-~+_.?#=&;, /:%!*\[\]()@]|i', '', $location); 
  15. $location = wp_kses_no_null($location); 
  16.  
  17. // remove %0d and %0a from location 
  18. $strip = array('%0d', '%0a', '%0D', '%0A'); 
  19. return _deep_replace( $strip, $location );