wp_logout_url()
云策文档标注
概述
wp_logout_url() 函数用于生成 WordPress 站点的用户登出 URL,支持自定义重定向路径,并通过 wp_nonce_url() 添加安全 nonce 参数。
关键要点
- 函数返回允许用户登出站点的 URL,可通过 $redirect 参数指定登出后的重定向路径。
- URL 经过 HTML 编码(通过 esc_html() 在 wp_nonce_url() 中处理),确保安全性。
- 支持 'logout_url' 过滤器钩子,允许开发者自定义登出 URL。
- 内部使用 site_url()、add_query_arg() 和 wp_nonce_url() 等函数构建 URL。
- 在 WordPress 2.7.0 版本中引入。
代码示例
// 默认使用,无重定向
wp_logout_url();
// 登出后重定向到首页
wp_logout_url( home_url() );
// 登出后重定向到自定义页面
wp_logout_url( 'https://example.com/custom-page' );
// 使用 allowed_redirect_hosts 过滤器允许重定向到多站点网络中的其他站点
add_filter( 'allowed_redirect_hosts', 'wpdocs_allow_ms_parent_redirect' );
function wpdocs_allow_ms_parent_redirect( $allowed ) {
$allowed[] = 'multisiteparent.com';
return $allowed;
}
// 通过 wp_logout 动作钩子在登出后重定向到首页
add_action( 'wp_logout', 'wpdocs_ahir_redirect_after_logout' );
function wpdocs_ahir_redirect_after_logout() {
wp_safe_redirect( home_url() );
exit();
}注意事项
- 重定向路径需确保在 allowed_redirect_hosts 过滤器允许的主机列表中,否则可能被阻止。
- URL 中的查询参数已编码,避免直接拼接字符串,建议使用 add_query_arg() 函数。
- 在模板中使用时,注意输出转义,例如通过 esc_url() 处理 URL。
原文内容
Retrieves the logout URL.
Description
Returns the URL that allows the user to log out of the site.
Parameters
$redirectstringrequired-
Path to redirect to on logout.
Source
function wp_logout_url( $redirect = '' ) {
$args = array();
if ( ! empty( $redirect ) ) {
$args['redirect_to'] = urlencode( $redirect );
}
$logout_url = add_query_arg( $args, site_url( 'wp-login.php?action=logout', 'login' ) );
$logout_url = wp_nonce_url( $logout_url, 'log-out' );
/**
* Filters the logout URL.
*
* @since 2.8.0
*
* @param string $logout_url The HTML-encoded logout URL.
* @param string $redirect Path to redirect to on logout.
*/
return apply_filters( 'logout_url', $logout_url, $redirect );
}
Hooks
- apply_filters( ‘logout_url’, string $logout_url, string $redirect )
-
Filters the logout URL.
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 8 content
Codex
This example shows how to logout and redirect to current page inside the loop.
<a href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a>Skip to note 9 content
Codex
This example shows how to logout and redirect to homepage.
<a href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>Skip to note 10 content
Codex
This example shows how to logout and redirect to another site. If you are using wp_logout_url to redirect to another site on logout (e.g. another subsite in a MultiSite network) you’ll need to make use of the allowed_redirect_hosts filter.
add_filter( 'allowed_redirect_hosts', 'wpdocs_allow_ms_parent_redirect' ); /** * Add host to redirection whitelist. * @param array $allowed Redirection allowed hosts. * @return array Extended redirection allowed hosts. */ function wpdocs_allow_ms_parent_redirect( $allowed ) { $allowed[] = 'multisiteparent.com'; return $allowed; } <a href="<?php echo wp_logout_url( 'http://multisiteparent.com' ); ?>">Logout</a>Skip to note 11 content
Codex
Default Usage.
<a href="<?php echo wp_logout_url(); ?>">Logout</a>Skip to note 12 content
Ahir Hemant
Logout redirect to home page
add_action( 'wp_logout','wpdocs_ahir_redirect_after_logout' ); function wpdocs_ahir_redirect_after_logout() { wp_safe_redirect( home_url() ); exit(); }Skip to note 13 content
WraithKenny
Default Safe Usage.
<a href="<?php echo esc_url( wp_logout_url() ); ?>">Logout</a>Skip to note 14 content
Cezar Ayran
I had to use a PHP function
to replace
& amp;with&or the warning won’t disappear.