返回介绍

wp_redirect()

发布于 2017-09-11 12:41:25 字数 5691 浏览 1046 评论 0 收藏 0

wp_redirect( string $location,  int $status = 302 )

Redirects to another page.


description

Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;:

wp_redirect( $url );
exit;

Exiting can also be selectively manipulated by using wp_redirect() as a conditional
in conjunction with the ‘wp_redirect’ and ‘wp_redirect_location’ hooks:

if ( wp_redirect( $url ) ) {
    exit;
}

参数

$location

(string) (Required) The path to redirect to.

$status

(int) (Optional) Status code to use.

Default value: 302


返回值

(bool) False if $location is not provided, true otherwise.


源代码

File: wp-includes/pluggable.php

function wp_redirect($location, $status = 302) {
	global $is_IIS;

	/**
	 * Filters the redirect location.
	 *
	 * @since 2.1.0
	 *
	 * @param string $location The path to redirect to.
	 * @param int    $status   Status code to use.
	 */
	$location = apply_filters( 'wp_redirect', $location, $status );

	/**
	 * Filters the redirect status code.
	 *
	 * @since 2.3.0
	 *
	 * @param int    $status   Status code to use.
	 * @param string $location The path to redirect to.
	 */
	$status = apply_filters( 'wp_redirect_status', $status, $location );

	if ( ! $location )
		return false;

	$location = wp_sanitize_redirect($location);

	if ( !$is_IIS && PHP_SAPI != 'cgi-fcgi' )
		status_header($status); // This causes problems on IIS and some FastCGI setups

	header("Location: $location", true, $status);

	return true;
}

更新日志

Versiondescription
1.5.1Introduced.

相关函数

Uses

  • wp-includes/pluggable.php: wp_sanitize_redirect()
  • wp-includes/pluggable.php: wp_redirect
  • wp-includes/pluggable.php: wp_redirect_status
  • wp-includes/functions.php: status_header()
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-admin/includes/media.php: wp_media_attach_action()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::set_pagination_args()
  • wp-admin/includes/dashboard.php: wp_dashboard_setup()
  • wp-admin/includes/plugin.php: activate_plugin()
  • wp-admin/includes/post.php: redirect_post()
  • wp-admin/update-core.php: do_dismiss_core_update()
  • wp-admin/update-core.php: do_undismiss_core_update()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::after_setup_theme()
  • wp-includes/cron.php: spawn_cron()
  • wp-includes/pluggable.php: wp_safe_redirect()
  • wp-includes/pluggable.php: auth_redirect()
  • wp-includes/query.php: wp_old_slug_redirect()
  • wp-includes/load.php: wp_not_installed()
  • wp-includes/canonical.php: redirect_canonical()
  • wp-includes/canonical.php: wp_redirect_admin_locations()
  • wp-includes/ms-functions.php: maybe_redirect_404()
  • wp-includes/ms-deprecated.php: wpmu_admin_do_redirect()
  • Show 12 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 9You must log in to vote on the helpfulness of this note Contributed by J.D. Grimes

    wp_redirect() does not validate that the $location is a reference to the current host. This means that this function is vulnerable to open redirects if you pass it a $location supplied by the user. For this reason, it is best practice to always use wp_safe_redirect() instead, since it will use wp_validate_redirect() to ensure that the $location refers to the current host. Only use wp_redirect() when you are specifically trying to redirect to another site, and then you can hard-code the URL.

    
    // We don't know for sure whether this is a URL for this site,
    // so we use wp_safe_redirect() to avoid an open redirect.
    wp_safe_redirect( $url );
    
    // We are trying to redirect to another site, using a hard-coded URL.
    wp_redirect( 'https://example.com/some/page' );
    
  2. Examples

    
    <?php wp_redirect( home_url() ); exit; ?>
    

    Redirects can also be external, and/or use a “Moved Permanently” code :

    
    <?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>
    

    The code below redirects to the parent post URL which can be used to redirect attachment pages back to the parent.

    
    <?php wp_redirect( get_permalink( $post->post_parent ) ); exit; ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文