wp_safe_redirect
Performs a safe (local) redirect, using wp_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 defaults to wp-admin on the siteurl instead. This prevents malicious redirects which redirect to another host, but only used in a few places.
Parameters (2)
- 0. $location (string)
- The path to redirect to.
- 1. $status — Optional. (int) =>
302
- Status code to use.
Usage
if ( !function_exists( 'wp_safe_redirect' ) ) { require_once ABSPATH . WPINC . '/pluggable.php'; } // The path to redirect to. $location = ''; // Status code to use. $status = 302; // NOTICE! Understand what this does before running. $result = wp_safe_redirect($location, $status);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/pluggable.php
- function wp_safe_redirect($location, $status = 302) {
- // Need to look at the URL the way it will end up in wp_redirect()
- $location = wp_sanitize_redirect($location);
- /**
- * Filters the redirect fallback URL for when the provided redirect is not safe (local).
- *
- * @since 4.3.0
- *
- * @param string $fallback_url The fallback URL to use by default.
- * @param int $status The redirect status.
- */
- $location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );
- wp_redirect($location, $status);
- }