钩子文档

user_profile_update_errors

💡 云策文档标注

概述

user_profile_update_errors 是一个 WordPress Hook,在用户资料更新错误返回前触发,允许开发者验证自定义字段并添加错误信息。

关键要点

  • 触发时机:在 edit_user_profile_update 和 personal_options_update 之后运行,用于验证用户资料更新前的错误。
  • 参数:$errors(WP_Error 对象,引用传递)、$update(布尔值,表示是否为用户更新)、$user(用户对象,引用传递)。
  • 功能:如果 $errors 对象包含错误,则保存操作不会完成,错误会显示给用户。
  • 用途:常用于验证自定义字段,通过检查 $errors 数组来确保数据有效性。

代码示例

function check_fields($errors, $update, $user) {
    $errors->add('demo_error',__('This is a demo error, and will halt profile save'));
}
add_action('user_profile_update_errors', 'check_fields', 10, 3);

注意事项

此 Hook 在验证自定义字段时,需在回调函数中检查 $errors 数组,如果为空则保存数据,否则阻止保存并显示错误。


📄 原文内容

Fires before user profile update errors are returned.

Parameters

$errorsWP_Error
WP_Error object (passed by reference).
$updatebool
Whether this is a user update.
$userstdClass
User object (passed by reference).

More Information

This hook runs AFTER edit_user_profile_update and personal_options_update. If you want to validate some custom fields before saving, a workaround is to check the $errors array in this same callback, after performing your validations, and save the data if it is empty.

On return, if the errors object contains errors then the save is not completed & the errors displayed to the user.

Source

do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes