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
Source
do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Collins Mbaka
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);