wp_validate_redirect( string $location, string $fallback_url = '' ): string
- Since
- 2.8.1
- Source
wp-includes/pluggable.php:1661
Description
Checks whether the $location is using an allowed host, if it has an absolute path. A plugin can therefore set or remove allowed host(s) to or from the list.
If the host is not allowed, then the redirect is to $fallback_url supplied.
Compatibility
- WordPress
- since 2.8.1
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$locationstring- The redirect to validate.
$fallback_urlstringoptional- The value to return if $location is not allowed.Default:
''
Return value
string- Redirect-sanitized URL.
Performance profile
How much work a call to wp_validate_redirect() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 20–111
- Plugin surface
- 1 hook
- Called by
- 7
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 126.
Third-party callbacks on 'allowed_redirect_hosts' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_validate_redirect() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 20–50 | wp_sanitize_redirect(), parse_url() |
$lp !== false && isset($lp[$component]) && strpbrk() | 37–61 | wp_sanitize_redirect(), parse_url(), strpbrk() |
$lp !== false && !empty($lp) && empty($value) | 46–62 | wp_sanitize_redirect(), parse_url(), ltrim() |
$lp !== false | 47–77 | wp_sanitize_redirect(), parse_url(), home_url(), parse_url() |
$lp !== false && !isset($lp) && !empty($lp) && empty($value) && isset($lp[$component]) && strpbrk() | 55–73 | wp_sanitize_redirect(), parse_url(), ltrim(), strpbrk() |
$lp !== false && isset($lp) && !$allowed_hosts | 58–85 | wp_sanitize_redirect(), parse_url(), home_url(), parse_url(), strtolower() |
$lp !== false && !empty($lp) && !empty($value) | 60–76 | wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim() |
$lp !== false && !isset($lp) && !empty($lp) && empty($value) | 65–89 | wp_sanitize_redirect(), parse_url(), ltrim(), home_url(), parse_url() |
$lp !== false && !isset($lp) && !empty($lp) && !empty($value) && isset($lp[$component]) && strpbrk() | 69–87 | wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim(), strpbrk() |
$lp !== false && !empty($lp) && empty($value) && !$allowed_hosts | 76–97 | wp_sanitize_redirect(), parse_url(), ltrim(), home_url(), parse_url(), strtolower() |
$lp !== false && !isset($lp) && !empty($lp) && !empty($value) | 79–103 | wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim(), home_url(), parse_url() |
$lp !== false && !empty($lp) && !empty($value) && !$allowed_hosts | 90–111 | wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim(), home_url(), parse_url(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 126 | 20–111 | 23 | |
| 8.5 | 126 | 20–111 | 23 | |
| 8.4 | 126 | 20–111 | 23 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 144 | 29–129 | 23 | |
| 8.2 | 144 | 29–129 | 23 | |
| 8.1 | 144 | 29–129 | 23 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 145 | 29–130 | 23 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_validate_redirect() runs, in this order:
- apply_filters( allowed_redirect_hosts )filterline 1721 (+60 into the body)
Filters the list of allowed hosts to redirect to.
Uses · 5
- wp_sanitize_redirect()Sanitizes a URL for use in a redirect.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_normalize_path()Normalizes a filesystem path.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 7
- WP_Customize_Manager::set_preview_url()Sets the initial URL to be previewed.
- WP_Customize_Manager::set_return_url()Sets URL to link the user to when closing the Customizer.
- allowed_http_request_hosts()Marks allowed redirect hosts safe for HTTP requests as well.
- wp_get_original_referer()Retrieves original referer that was posted, if it exists.
- wp_get_referer()Retrieves referer from '_wp_http_referer' or HTTP referer.
- wp_nonce_ays()Displays "Are You Sure" message to confirm the action being taken.
- wp_safe_redirect()Performs a safe (local) redirect, using wp_redirect().
Source code
function wp_validate_redirect( $location, $fallback_url = '' ) { $location = wp_sanitize_redirect( trim( $location, " \t\n\r\0\x08\x0B" ) ); // Browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'. if ( str_starts_with( $location, '//' ) ) { $location = 'http:' . $location; } /* * In PHP 5 parse_url() may fail if the URL query part contains 'http://'. * See https://bugs.php.net/bug.php?id=38143 */ $cut = strpos( $location, '?' ); $test = $cut ? substr( $location, 0, $cut ) : $location; $lp = parse_url( $test ); // Give up if malformed URL. if ( false === $lp ) { return $fallback_url; } // Allow only 'http' and 'https' schemes. No 'data:', etc. if ( isset( $lp['scheme'] ) && ! ( 'http' === $lp['scheme'] || 'https' === $lp['scheme'] ) ) { return $fallback_url; } if ( ! isset( $lp['host'] ) && ! empty( $lp['path'] ) && '/' !== $lp['path'][0] ) { $path = ''; if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { $path = dirname( parse_url( 'http://placeholder' . $_SERVER['REQUEST_URI'], PHP_URL_PATH ) . '?' ); $path = wp_normalize_path( $path ); } $location = '/' . ltrim( $path . '/', '/' ) . $location; } /* * Reject if certain components are set but host is not. * This catches URLs like https:host.com for which parse_url() does not set the host field. */ if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) { return $fallback_url; } // Reject malformed components parse_url() can return on odd inputs. foreach ( array( 'user', 'pass', 'host' ) as $component ) { if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) { return $fallback_url; } } $wpp = parse_url( home_url() ); /** * Filters the list of allowed hosts to redirect to. * * @since 2.3.0 * * @param string[] $hosts An array of allowed host names. * @param string $host The host name of the redirect destination; empty string if not set. */ $allowed_hosts = (array) apply_filters( 'allowed_redirect_hosts', array( $wpp['host'] ), isset( $lp['host'] ) ? $lp['host'] : '' ); if ( isset( $lp['host'] ) && ( ! in_array( $lp['host'], $allowed_hosts, true ) && strtolower( $wpp['host'] ) !== $lp['host'] ) ) { $location = $fallback_url; } return $location; }Changelog
Introduced in 2.8.1. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.