wp_validate_redirect()
Checks the specified URL for the possibility of using it for redirection.
By default, only URLs of the current site are allowed (internal URL addresses).
If the specified URL fails the check, the function will return the value of the second parameter $default.
What the function does:
- Adds
http:protocol for links without a protocol (//) - Checks the protocol, only
httpandhttpsare allowed. - Checks the other components of the URL.
- Compares the domain of the specified URL with the whitelist of domains to which redirection is allowed. By default, the whitelist contains only the domain of the current site. The whitelist can be expanded through the filter allowed_redirect_hosts.
To sanitize the redirect link, use wp_sanitize_redirect()
Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.
Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.
Hooks from the function
Returns
String. The specified URL address if it passes the check or the value of the $default parameter.
Usage
wp_validate_redirect( $location, $default );
- $location(string) (required)
- URL to check.
- $default(string)
- Default URL that the function will return if the URL from the $location parameter fails the check.
Default: ''
Examples
#1 Demo
It is assumed that the function was launched at example.com.
echo wp_validate_redirect( 'http://foo.bar' ); //> '' echo wp_validate_redirect( 'http://foo.bar', 'http://my.site' ); //> http://my.site echo wp_validate_redirect( '//example.com/foo' ); //> http://example.ru/foo echo wp_validate_redirect( 'https://example.com/foo' ); //> https://example.ru/foo
Changelog
| Since 2.8.1 | Introduced. |