wp_safe_redirect

函数
wp_safe_redirect ( $location, $status = 302, $x_redirect_by = 'WordPress' )
参数
  • (string) $location The path or URL to redirect to.
    Required:
  • (int) $status Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
    Required:
    Default: 302
  • (string) $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
    Required:
    Default: 'WordPress'
返回值
  • (bool) False if the redirect was canceled, true otherwise.
定义位置
相关方法
wp_sanitize_redirectwp_redirectwp_validate_redirectis_redirectwp_old_slug_redirect
引入
2.3.0
弃用
-

使用wp_redirect()执行一个安全的(本地)重定向。

检查$location是否使用了允许的主机,如果它有一个绝对路径。因此,一个插件可以设置或删除允许的主机,或从列表中删除。

如果主机不允许,那么重定向就会默认为siteurl上的wp-admin。这可以防止重定向到另一个主机的恶意重定向,但只在少数地方使用。

注意:wp_safe_redirect()不会自动退出,几乎总是应该在后面调用`exit;`。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wp_safe_redirect( $url ); exit;
wp_safe_redirect( $url ); exit;
wp_safe_redirect( $url ); exit;

通过使用wp_safe_redirect()作为条件,结合{@see ‘wp_redirect’}和{@see ‘wp_redirect_location’}过滤器,也可以对退出进行选择性操作。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if ( wp_safe_redirect( $url ) ) { exit; }
if ( wp_safe_redirect( $url ) ) { exit; }
if ( wp_safe_redirect( $url ) ) { exit; }

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
// 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 HTTP response status code to use.
*/
$location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );
return wp_redirect( $location, $status, $x_redirect_by );
}
endif;
if ( ! function_exists( '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.
*
* @since 2.8.1
*
* @param string $location The redirect to validate.
* @param string $default The value to return if $location is not allowed.
* @return string redirect-sanitized URL.
*/
function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) { // 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 HTTP response status code to use. */ $location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) ); return wp_redirect( $location, $status, $x_redirect_by ); } endif; if ( ! function_exists( '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. * * @since 2.8.1 * * @param string $location The redirect to validate. * @param string $default The value to return if $location is not allowed. * @return string redirect-sanitized URL. */
function wp_safe_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {

		// 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 HTTP response status code to use.
		 */
		$location = wp_validate_redirect( $location, apply_filters( 'wp_safe_redirect_fallback', admin_url(), $status ) );

		return wp_redirect( $location, $status, $x_redirect_by );
	}
endif;

if ( ! function_exists( '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.
	 *
	 * @since 2.8.1
	 *
	 * @param string $location The redirect to validate.
	 * @param string $default  The value to return if $location is not allowed.
	 * @return string redirect-sanitized URL.
	 */

常见问题

FAQs
查看更多 >