钩子文档

wp_login_errors

💡 云策文档标注

概述

wp_login_errors 是一个 WordPress 过滤器,用于修改登录页面上的错误消息。它允许开发者自定义或调整登录过程中显示的错误信息,例如注册成功消息。

关键要点

  • 过滤器名称:wp_login_errors,用于过滤登录页面的错误。
  • 参数:接受两个参数,$errors(WP_Error 对象)和 $redirect_to(重定向 URL)。
  • 用途:常用于修改注册成功后的提示消息,例如移除或替换默认消息。
  • 版本:自 WordPress 3.6.0 引入。

代码示例

add_filter( 'wp_login_errors', 'wpdocs_filter_wp_login_errors', 10, 2 );

function wpdocs_filter_wp_login_errors( $errors, $redirect_to ) {
    if ( strpos( $_SERVER['REQUEST_URI'], 'checkemail=registered' ) !== false ) {
        $errors->remove( 'registered' );
        $errors->add( 'registered', '自定义消息', 'message' );
    }
    return $errors;
}

注意事项

  • 确保正确处理 WP_Error 对象,避免破坏其他错误消息。
  • 在修改消息时,注意检查条件(如 URL 参数),以避免影响非注册流程。
  • 代码示例中展示了两种参数用法:带 $redirect_to 和不带,需根据实际需求选择。

📄 原文内容

Filters the login page errors.

Parameters

$errorsWP_Error
WP Error object.
$redirect_tostring
Redirect destination URL.

Source

$errors = apply_filters( 'wp_login_errors', $errors, $redirect_to );

Changelog

Version Description
3.6.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    /**
     * Change the Register Success message 
     * @param WP_Error	$wp_error The WP_Error object.
     * @param string		Redirect destination URL.
     * 
     * @return WP_Error	$wp_error The WP_Error object.
     */
    add_filter( 'wp_login_errors', 'wpdocs_filter_wp_login_errors', 10, 2 );
    
    function wpdocs_filter_wp_login_errors( $errors, $redirect_to ) {
    	// Run Only if user registration is a success
    	if ( strpos( $_SERVER['REQUEST_URI'], 'checkemail=registered' ) !== false ) {
    
    		// Remove the existing Register success message
    		$errors->remove( 'registered' );
    
    		// make the changes to register message
    		$errors->add( 'registered', sprintf( __( 'Please check your Spam box if email is not to be found in inbox.' ), wp_login_url() ), 'message' );
    
    		return $errors;
    	} else {
    		// Return the other message and errors
    		return $errors;
    	}
    }

  2. Skip to note 4 content

    /**
     * Change the Register Success message 
     * 
     * @param WP_Error $wp_error The WP_Error object.
     * @return WP_Error $wp_error The WP_Error object.
     */
    add_filter( 'wp_login_errors', 'wpdocs_filter_wp_login_errors' );
    
    function wpdocs_filter_wp_login_errors( $errors ) {
    	// Run Only if user registration is a success
    	if ( strpos( $_SERVER['REQUEST_URI'], 'checkemail=registered' ) !== false ) {
    
    		// Remove the existing Register success message
    		$errors->remove( 'registered' );
    
    		// make the changes to register message
    		$errors->add( 'registered', __( 'Please check your Spam box if email is not to be found in inbox.' ), 'message' );
    	}
    
    	return $errors;
    }