函数文档

wp_auth_check_load()

💡 云策文档标注

概述

wp_auth_check_load() 函数用于加载用户登录状态监控的认证检查,通过 enqueue 样式和脚本来实现。该功能可通过过滤器或移除动作进行控制,并针对特定屏幕禁用以避免干扰。

关键要点

  • 函数 wp_auth_check_load() 在非管理员或未登录用户时返回,不加载认证检查。
  • 在 IFRAME_REQUEST 定义时也返回,避免在 iframe 中加载。
  • 默认隐藏特定屏幕(如更新、升级、网络相关页面),通过 $hidden 数组定义。
  • 使用过滤器 'wp_auth_check_load' 可精细控制是否加载认证检查,接受 $show 和 $screen 参数。
  • 若过滤器返回真值,则 enqueue 'wp-auth-check' 样式和脚本,并添加动作在页脚输出 HTML。
  • 可通过 remove_action('admin_enqueue_scripts', 'wp_auth_check_load') 完全禁用此功能。

代码示例

function wp_auth_check_load() {
    if ( ! is_admin() && ! is_user_logged_in() ) {
        return;
    }

    if ( defined( 'IFRAME_REQUEST' ) ) {
        return;
    }

    $screen = get_current_screen();
    $hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
    $show   = ! in_array( $screen->id, $hidden, true );

    if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
        wp_enqueue_style( 'wp-auth-check' );
        wp_enqueue_script( 'wp-auth-check' );

        add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
        add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
    }
}

注意事项

  • 此功能自 WordPress 3.6.0 版本引入。
  • 在特定屏幕(如更新页面)默认禁用,以防止登录屏幕造成不便中断。
  • 开发者可利用过滤器 'wp_auth_check_load' 自定义加载逻辑,例如基于用户角色或屏幕条件。

📄 原文内容

Loads the auth check for monitoring whether the user is still logged in.

Description

Can be disabled with remove_action( ‘admin_enqueue_scripts’, ‘wp_auth_check_load’ );

This is disabled for certain screens where a login screen could cause an inconvenient interruption. A filter called ‘wp_auth_check_load’ can be used for fine-grained control.

Source

function wp_auth_check_load() {
	if ( ! is_admin() && ! is_user_logged_in() ) {
		return;
	}

	if ( defined( 'IFRAME_REQUEST' ) ) {
		return;
	}

	$screen = get_current_screen();
	$hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
	$show   = ! in_array( $screen->id, $hidden, true );

	/**
	 * Filters whether to load the authentication check.
	 *
	 * Returning a falsey value from the filter will effectively short-circuit
	 * loading the authentication check.
	 *
	 * @since 3.6.0
	 *
	 * @param bool      $show   Whether to load the authentication check.
	 * @param WP_Screen $screen The current screen object.
	 */
	if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
		wp_enqueue_style( 'wp-auth-check' );
		wp_enqueue_script( 'wp-auth-check' );

		add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
		add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
	}
}

Hooks

apply_filters( ‘wp_auth_check_load’, bool $show, WP_Screen $screen )

Filters whether to load the authentication check.

Changelog

Version Description
3.6.0 Introduced.