user_register
云策文档标注
概述
user_register 是一个 WordPress 动作钩子,在新用户注册后立即触发,允许开发者访问新用户的 ID 和数据。它常用于保存自定义注册表单传递的额外用户元数据。
关键要点
- 触发时机:新用户注册后立即触发,用户 ID 已添加到数据库。
- 参数:$user_id(用户 ID)和 $userdata(传递给 wp_insert_user() 的原始数据数组)。
- 注意事项:触发时并非所有用户元数据都已存储(如 first_name 和 last_name 可能未保存),密码已加密,验证应在 registration_errors 钩子中进行。
- 典型用途:保存自定义注册字段、发送欢迎邮件或更新用户数据。
代码示例
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
if ( isset( $_POST['first_name'] ) )
update_user_meta($user_id, 'first_name', $_POST['first_name']);
}注意事项
- 避免在 user_register 钩子中进行字段验证,应使用 registration_errors 钩子。
- 注意用户数据存储状态,部分元数据可能尚未保存到数据库。
原文内容
Fires immediately after a new user is registered.
Parameters
$user_idint-
User ID.
$userdataarray-
The raw array of data passed to wp_insert_user() .
More Arguments from wp_insert_user( … $userdata )
An array, object, or WP_User object of user data arguments.
IDintUser ID. If supplied, the user will be updated.user_passstringThe plain-text user password for new users.
Hashed password for existing users.user_loginstringThe user’s login username.user_nicenamestringThe URL-friendly user name.user_urlstringThe user URL.user_emailstringThe user email address.display_namestringThe user’s display name.
Default is the user’s username.nicknamestringThe user’s nickname.
Default is the user’s username.first_namestringThe user’s first name. For new users, will be used to build the first part of the user’s display name if$display_nameis not specified.last_namestringThe user’s last name. For new users, will be used to build the second part of the user’s display name if$display_nameis not specified.descriptionstringThe user’s biographical description.rich_editingstringWhether to enable the rich-editor for the user.
Accepts'true'or'false'as a string literal, not boolean. Default'true'.syntax_highlightingstringWhether to enable the rich code editor for the user.
Accepts'true'or'false'as a string literal, not boolean. Default'true'.comment_shortcutsstringWhether to enable comment moderation keyboard shortcuts for the user. Accepts'true'or'false'as a string literal, not boolean. Default'false'.admin_colorstringAdmin color scheme for the user. Default'fresh'.use_sslboolWhether the user should always access the admin over https. Default false.user_registeredstringDate the user registered in UTC. Format is ‘Y-m-d H:i:s’.user_activation_keystringPassword reset key. Default empty.spamboolMultisite only. Whether the user is marked as spam.
Default false.show_admin_bar_frontstringWhether to display the Admin Bar for the user on the site’s front end. Accepts'true'or'false'as a string literal, not boolean. Default'true'.rolestringUser’s role.localestringUser’s locale. Default empty.meta_inputarrayArray of custom user meta values keyed by meta key.
Default empty.
Source
do_action( 'user_register', $user_id, $userdata );
Skip to note 4 content
Steven Lin
Example migrated from Codex:
This example will save a
first_namefield passed by a custom registration field.Also, keep in mind that validation of registration fields should not be performed within this hook! Validate using the
registration_errorshook instead (theuser_registerhook will not be called ifregistration_errorsvalidation fails).add_action( 'user_register', 'myplugin_registration_save', 10, 1 ); function myplugin_registration_save( $user_id ) { if ( isset( $_POST['first_name'] ) ) update_user_meta($user_id, 'first_name', $_POST['first_name']); }Skip to note 5 content
Omid Beheshtian
We can use this php code to update database from
own custom register field.
add_action( 'user_register', function ( $user_id ) { $userdata = array(); $userdata['ID'] = $user_id; $userdata['telnumber'] = $_POST['telnumber']; wp_update_user( $userdata ); } );Skip to note 6 content
Armin Amiri Nasab
In this code we send an email when user register at our site:
add_action( 'user_register', function ( $userID, $userData ) { wp_mail( $userData['user_email'], __( 'Welcome' ) . ' ' . $userData['nickname'], __( 'Welcome to our site :)' ) ); }, 10, 2 );