钩子文档

user_registration_email

💡 云策文档标注

概述

user_registration_email 是一个 WordPress 过滤器,用于在用户注册过程中过滤新用户的电子邮件地址。它位于 register_new_user() 函数的起始位置,允许开发者在数据进一步处理前修改电子邮件字段或相关 $_POST 数据。

关键要点

  • 过滤器名称:user_registration_email,参数为 $user_email(字符串类型),表示新用户的电子邮件地址。
  • 钩子位置:在 wp-login.php 的 register_new_user() 函数中,用户数据经过清理后立即调用,是注册流程中的第一个过滤器。
  • 用途:可用于自定义电子邮件地址,例如将其设置为与用户名相同,或进行其他表单定制操作。
  • 注意事项:由于 register_new_user() 可能在没有表单提交的情况下调用,依赖 $_POST 数据可能导致问题。

代码示例

function wpdocs_use_email_as_username( $user_email ) {
    return $_POST['user_login'];
}

add_filter( 'user_registration_email', 'wpdocs_use_email_as_username' );

📄 原文内容

Filters the email address of a user being registered.

Parameters

$user_emailstring
The email address of the new user.

More Information

This filter hooks into the very start of the register_new_user() function of wp-login.php after the user has been sanitized and it is used to manipulate the value submitted for user_email.

As it is the very first filter to be called in the registration process it’s a reasonable (i.e. not really good but possible) place to manipulate user registration data in $_POST as well as the email field itself before that data is further processed.

This means that it can be used for instance to set the email address field to be the same as the username (which on your registration form you could label as email address if you wanted to), and do other more interesting form customizations.

Source

$user_email = apply_filters( 'user_registration_email', $user_email );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    An Example

    function wpdocs_use_email_as_username( $user_email ) {
    	return $_POST['user_login'];
    }
    
    add_filter( 'user_registration_email', 'wpdocs_use_email_as_username' );