函数文档

login_header()

💡 云策文档标注

概述

login_header() 函数用于输出 WordPress 登录页面的头部内容,包括 HTML 结构、标题、错误消息和样式。它处理登录页面的基本渲染和错误显示逻辑。

关键要点

  • 函数接受三个可选参数:$title(登录页面标题,默认 'Log In')、$message(头部消息,默认空)、$wp_error(WP_Error 实例,默认 null)。
  • 自动添加防止索引的过滤器(如 wp_robots_sensitive_page)和登录页面特定的动作(如 wp_login_viewport_meta)。
  • 根据错误代码(如 'empty_password')触发登录表单的抖动效果,通过 shake_error_codes 过滤器可自定义。
  • 生成并过滤登录页面标题(login_title 过滤器),支持恢复模式显示。
  • 输出 HTML 头部,包括语言属性、字符集、视口 meta 标签,并引入登录样式(login.css)。
  • 处理错误消息显示:通过 WP_Error 实例管理错误,使用 login_errors 和 login_messages 过滤器自定义错误和信息消息。
  • 包含多个相关 Hook,如 login_head、login_header、login_headerurl 等,用于扩展登录页面功能。

代码示例

$errors = new WP_Error();
// ...
login_header( __( 'Reset Password', 'textdomain' ), '<p>' . __( 'Enter your new password below.', 'textdomain' ) . '</p>', $errors );

注意事项

  • 函数内部使用全局变量 $error、$interim_login、$action,需注意其状态管理。
  • 错误处理依赖于 WP_Error 类,确保正确初始化和传递错误实例。
  • 通过过滤器(如 shake_error_codes、login_title)可高度自定义登录页面行为,开发时应遵循 WordPress 编码标准。

📄 原文内容

Outputs the login page header.

Parameters

$titlestring|nulloptional
WordPress login page title to display in the <title> element.
Defaults to ‘Log In’.

Default:null

$messagestringoptional
Message to display in header. Default empty.
$wp_errorWP_Error|nulloptional
The error to pass. Defaults to a WP_Error instance.

Default:null

Source

function login_header( $title = null, $message = '', $wp_error = null ) {
	global $error, $interim_login, $action;

	if ( null === $title ) {
		$title = __( 'Log In' );
	}

	// Don't index any of these forms.
	add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
	add_action( 'login_head', 'wp_strict_cross_origin_referrer' );

	add_action( 'login_head', 'wp_login_viewport_meta' );

	if ( ! is_wp_error( $wp_error ) ) {
		$wp_error = new WP_Error();
	}

	// Shake it!
	$shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password', 'retrieve_password_email_failure' );
	/**
	 * Filters the error codes array for shaking the login form.
	 *
	 * @since 3.0.0
	 *
	 * @param string[] $shake_error_codes Error codes that shake the login form.
	 */
	$shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );

	if ( $shake_error_codes && $wp_error->has_errors() && in_array( $wp_error->get_error_code(), $shake_error_codes, true ) ) {
		add_action( 'login_footer', 'wp_shake_js', 12 );
	}

	$login_title = get_bloginfo( 'name', 'display' );

	/* translators: Login screen title. 1: Login screen name, 2: Network or site name. */
	$login_title = sprintf( __( '%1$s ‹ %2$s — WordPress' ), $title, $login_title );

	if ( wp_is_recovery_mode() ) {
		/* translators: %s: Login screen title. */
		$login_title = sprintf( __( 'Recovery Mode — %s' ), $login_title );
	}

	/**
	 * Filters the title tag content for login page.
	 *
	 * @since 4.9.0
	 *
	 * @param string $login_title The page title, with extra context added.
	 * @param string $title       The original page title.
	 */
	$login_title = apply_filters( 'login_title', $login_title, $title );

	?>
	<html <?php language_attributes(); ?>>
	<head>
	<meta http-equiv="Content-Type" content="<?php bloginfo( 'html_type' ); ?>; charset=<?php bloginfo( 'charset' ); ?>" />
	<title></title>
	get_error_code() ) {
		ob_start();
		?>
		<script>if("sessionStorage" in window){try{for(var key in sessionStorage){if(key.indexOf("wp-autosave-")!=-1){sessionStorage.removeItem(key)}}}catch(e){}};</script>
		
		<style type="text/css">html{background-color: transparent;}</style>
		
	</head>
	<body class="login no-js <?php echo esc_attr( implode( ' ', $classes ) ); ?>">
	

	
	
		<h1 class="screen-reader-text"></h1>
		
	<div id="login">
		<h1 role="presentation" class="wp-login-logo"><a href="<?php echo esc_url( $login_header_url ); ?>"></a></h1>
	add( 'error', $error );
		unset( $error );
	}

	if ( $wp_error->has_errors() ) {
		$error_list = array();
		$messages   = '';

		foreach ( $wp_error->get_error_codes() as $code ) {
			$severity = $wp_error->get_error_data( $code );
			foreach ( $wp_error->get_error_messages( $code ) as $error_message ) {
				if ( 'message' === $severity ) {
					$messages .= '<p>' . $error_message . '</p>';
				} else {
					$error_list[] = $error_message;
				}
			}
		}

		if ( ! empty( $error_list ) ) {
			$errors = '';

			if ( count( $error_list ) > 1 ) {
				$errors .= '<ul class="login-error-list">';

				foreach ( $error_list as $item ) {
					$errors .= '<li>' . $item . '</li>';
				}

				$errors .= '</ul>';
			} else {
				$errors .= '<p>' . $error_list[0] . '</p>';
			}

			/**
			 * Filters the error messages displayed above the login form.
			 *
			 * @since 2.1.0
			 *
			 * @param string $errors Login error messages.
			 */
			$errors = apply_filters( 'login_errors', $errors );

			wp_admin_notice(
				$errors,
				array(
					'type'           => 'error',
					'id'             => 'login_error',
					'paragraph_wrap' => false,
				)
			);
		}

		if ( ! empty( $messages ) ) {
			/**
			 * Filters instructional messages displayed above the login form.
			 *
			 * @since 2.5.0
			 *
			 * @param string $messages Login messages.
			 */
			$messages = apply_filters( 'login_messages', $messages );

			wp_admin_notice(
				$messages,
				array(
					'type'               => 'info',
					'id'                 => 'login-message',
					'additional_classes' => array( 'message' ),
					'paragraph_wrap'     => false,
				)
			);
		}
	}
} // End of login_header().

Hooks

apply_filters( ‘login_body_class’, string[] $classes, string $action )

Filters the login page body classes.

do_action( ‘login_enqueue_scripts’ )

Enqueues scripts and styles for the login page.

apply_filters( ‘login_errors’, string $errors )

Filters the error messages displayed above the login form.

do_action( ‘login_head’ )

Fires in the login page header after scripts are enqueued.

do_action( ‘login_header’ )

Fires in the login page header after the body tag is opened.

apply_filters( ‘login_headertext’, string $login_header_text )

Filters the link text of the header logo above the login form.

apply_filters_deprecated( ‘login_headertitle’, string $login_header_title )

Filters the title attribute of the header logo above login form.

apply_filters( ‘login_headerurl’, string $login_header_url )

Filters link URL of the header logo above login form.

apply_filters( ‘login_message’, string $message )

Filters the message to display above the login form.

apply_filters( ‘login_messages’, string $messages )

Filters instructional messages displayed above the login form.

apply_filters( ‘login_title’, string $login_title, string $title )

Filters the title tag content for login page.

apply_filters( ‘shake_error_codes’, string[] $shake_error_codes )

Filters the error codes array for shaking the login form.

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.