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
檢視更多 >