wp_validate_redirect()
Validates a URL for use in a redirect.
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 $default supplied.
This is a pluggable function, and it can be replaced by 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 a plugin you can create a function with the same name, then it replace this function.
Hooks from the function
Return
String
. redirect-sanitized URL.
Usage
wp_validate_redirect( $location, $default );
- $location(string) (required)
- The redirect to validate.
- $default(string)
- The value to return if $location is not allowed.
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. |