wp_auth_check_html()
概述
wp_auth_check_html() 函数用于在用户登录状态失效时输出显示 wp-login 对话框的 HTML 代码。它通过检查登录 URL 是否与当前域相同,并应用过滤器来调整输出。
关键要点
- 函数输出包含登录对话框的 HTML,用于用户重新认证。
- 使用 wp_login_url() 获取登录 URL,并通过 is_ssl() 和 $_SERVER['HTTP_HOST'] 判断当前域。
- 应用 wp_auth_check_same_domain 过滤器来修改 $same_domain 值,影响 CSS 类名。
- 输出中包含国际化字符串(如 _e())、URL 清理(esc_url())和查询参数添加(add_query_arg())。
- 相关函数包括 get_user_locale()、wp_login_url()、is_ssl() 等。
代码示例
function wp_auth_check_html() {
$login_url = wp_login_url();
$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
$same_domain = str_starts_with( $login_url, $current_domain );
$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
$wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
// HTML 输出代码省略
}注意事项
- 该函数自 WordPress 3.6.0 版本引入。
- 输出 HTML 包含条件类名,依赖于 $same_domain 值。
- 使用多个辅助函数确保安全性和国际化,如 esc_url() 和 _e()。
Outputs the HTML that shows the wp-login dialog when the user is no longer logged in.
Source
function wp_auth_check_html() {
$login_url = wp_login_url();
$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
$same_domain = str_starts_with( $login_url, $current_domain );
/**
* Filters whether the authentication check originated at the same domain.
*
* @since 3.6.0
*
* @param bool $same_domain Whether the authentication check originated at the same domain.
*/
$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
$wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
?>
<div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
<div id="wp-auth-check-bg"></div>
<div id="wp-auth-check">
<button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text">
</span></button>
'1',
'wp_lang' => get_user_locale(),
),
$login_url
);
?>
<div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( $login_src ); ?>"></div>
<div class="wp-auth-fallback">
<p><b class="wp-auth-fallback-expired" tabindex="0"></b></p>
<p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"></a>
</p>
</div>
</div>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/functions.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/functions.php#L7465">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/functions.php#L7465-L7514">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/wp_auth_check_same_domain/"><span class="hook-func">apply_filters</span>( ‘wp_auth_check_same_domain’, <nobr><span class="arg-type">bool</span> <span class="arg-name">$same_domain</span></nobr> )</a></dt><dd><p>Filters whether the authentication check originated at the same domain.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_user_locale/">get_user_locale()</a><code>wp-includes/l10n.php
Retrieves the locale of a user.
wp_login_url()wp-includes/general-template.php
Retrieves the login URL.
is_ssl()wp-includes/load.php
Determines if SSL is used.
_e()wp-includes/l10n.php
Displays translated text.
esc_url()wp-includes/formatting.php
Checks and cleans a URL.
add_query_arg()wp-includes/functions.php
Retrieves a modified URL query string.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |