wppaste
WordPress

wp_validate_redirect( string $location, string $fallback_url = '' ): string

Since
2.8.1
Source
wp-includes/pluggable.php:1565
Validates a URL for use in a redirect.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
20–111

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 126.

Plugin surface
1 hook

Third-party callbacks on 'allowed_redirect_hosts' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always20–50wp_sanitize_redirect(), parse_url()
$lp !== false && isset($lp[$component]) && strpbrk()37–61wp_sanitize_redirect(), parse_url(), strpbrk()
$lp !== false && !empty($lp) && empty($value)46–62wp_sanitize_redirect(), parse_url(), ltrim()
$lp !== false47–77wp_sanitize_redirect(), parse_url(), home_url(), parse_url()
$lp !== false && !isset($lp) && !empty($lp) && empty($value) && isset($lp[$component]) && strpbrk()55–73wp_sanitize_redirect(), parse_url(), ltrim(), strpbrk()
$lp !== false && isset($lp) && !$allowed_hosts58–85wp_sanitize_redirect(), parse_url(), home_url(), parse_url(), strtolower()
$lp !== false && !empty($lp) && !empty($value)60–76wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim()
$lp !== false && !isset($lp) && !empty($lp) && empty($value)65–89wp_sanitize_redirect(), parse_url(), ltrim(), home_url(), parse_url()
$lp !== false && !isset($lp) && !empty($lp) && !empty($value) && isset($lp[$component]) && strpbrk()69–87wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim(), strpbrk()
$lp !== false && !empty($lp) && empty($value) && !$allowed_hosts76–97wp_sanitize_redirect(), parse_url(), ltrim(), home_url(), parse_url(), strtolower()
$lp !== false && !isset($lp) && !empty($lp) && !empty($value)79–103wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim(), home_url(), parse_url()
$lp !== false && !empty($lp) && !empty($value) && !$allowed_hosts90–111wp_sanitize_redirect(), parse_url(), parse_url(), wp_normalize_path(), ltrim(), home_url(), parse_url(), strtolower()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12620–11123
8.512620–11123
8.412620–1112318 fewer instructions than PHP 8.3
8.314429–12923
8.214429–12923
8.114429–129231 fewer instruction than PHP 7.4
7.414529–13023

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:

  1. apply_filters( allowed_redirect_hosts )filterline 1625 (+60 into the body)

    Filters the list of allowed hosts to redirect to.

Uses · 5

Used by · 7

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.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.