profile_update
云策文档标注
概述
profile_update 是一个 WordPress 动作钩子,在现有用户更新后立即触发。它允许开发者在用户资料变更时执行自定义代码,常用于日志记录、数据同步或验证操作。
关键要点
- 触发时机:用户更新操作完成后立即执行。
- 参数:$user_id(用户ID)、$old_user_data(更新前的用户数据对象)、$userdata(传递给 wp_insert_user() 的原始数据数组)。
- 用途:可用于检测用户字段变化(如邮箱)、处理重定向或集成第三方服务。
- 注意事项:在密码重置等场景中可能被调用,需谨慎处理以避免功能中断;$userdata 参数不包含 meta_input 自定义元数据。
代码示例
add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {
// 检测邮箱是否更新
$old_email = $old_user_data->data->user_email;
$user = get_userdata( $user_id );
$new_email = $user->user_email;
if ( $new_email !== $old_email ) {
// 执行操作
}
}
原文内容
Fires immediately after an existing user is updated.
Parameters
$user_idint-
User ID.
$old_user_dataWP_User-
Object containing user’s data prior to update.
$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( 'profile_update', $user_id, $old_user_data, $userdata );
Skip to note 4 content
Collins Mbaka
Basic Usage
add_action( 'profile_update', 'my_profile_update', 10, 2 ); function my_profile_update( $user_id, $old_user_data ) { // Do something }Skip to note 5 content
Uriahs Victor
Finding out if a user email was updated from the admin dashboard, can work for other fields as well if value being checked is changed:
function wpdocs_check_user_email_updated( $user_id, $old_user_data ) { $old_user_email = $old_user_data->data->user_email; $user = get_userdata( $user_id ); $new_user_email = $user->user_email; if ( $new_user_email !== $old_user_email ) { // Do something if old and new email aren't the same } } add_action( 'profile_update', 'wpdocs_check_user_email_updated', 10, 2 );Skip to note 6 content
wombley
Note if you’re using this hook to do a redirect once a user’s set their profile details, it can break the lost password functionality as that sends the email AFTER updating the profile with a new code. Example fix, by checking if user_activation_key has changed:
add_action( 'profile_update', 'wpdocs_profile_update', 10, 3 ); function wpdocs_profile_update( $user_id, $oldUserData, $newUserData ) { // This is also called on lost password, BEFORE the email is sent - hence breaking it. If that's the case, don't redirect. $doRedirect = $oldUserData->data->user_activation_key === $newUserData['user_activation_key']; if ( $doRedirect && get_option( 'wpdocs-redirect-users-to-on-login', '' ) ) { wp_redirect( get_option( 'wpdocs-redirect-users-to-on-login', '' ) ); die(); } }