钩子文档

edit_user_profile_update

💡 云策文档标注

概述

edit_user_profile_update 是一个 WordPress 动作钩子,在“编辑用户”页面加载前触发,主要用于保存添加到用户资料页的自定义字段。此钩子仅在用户查看其他用户(非自己)的资料页时触发。

关键要点

  • 触发时机:在“编辑用户”页面加载前触发,适用于保存自定义字段。
  • 参数:$user_id(整数类型),表示用户 ID。
  • 适用范围:仅当用户查看其他用户的资料页时触发;如需应用于所有资料页(包括当前用户),应使用 personal_options_update 钩子。
  • 注意事项:在 HTML 表单中,自定义元字段的 name 属性应避免与用户元键名相同,否则可能导致 $_POST 数据为空;建议添加后缀以区分。

代码示例

function update_extra_profile_fields($user_id) {
    if ( current_user_can('edit_user',$user_id) )
        update_user_meta($user_id, 'my_custom_field', $_POST['your_field']);
}
add_action('edit_user_profile_update', 'update_extra_profile_fields');

📄 原文内容

Fires before the page loads on the ‘Edit User’ screen.

Parameters

$user_idint
The user ID.

More Information

This action hook is generally used to save custom fields that have been added to the WordPress profile page.

This hook only triggers when a user is viewing another user’s profile page (not their own). If you want to apply your hook to ALL profile pages (including the current user), you need to use the personal_options_update hook.

Note on the the html element name attribute for the “custom meta field”:
Consider the example:
update_user_meta($user_id, 'custom_meta_key', $_POST['custom_meta_key']);
Make sure that you give a different key name for the $_POST data key and the actual user meta key. If you use the same key for both, WordPress for some reason empties the value posted under that key and you’ll always get an empty value in $_POST[‘custom_meta_key’]. To prevent this, you may change the text in the html input element name attribute and append a suffix. After changing, you $_POST data for the custom meta field will be accessible in $_POST[‘custom_meta_key_data’] and shall pass the data properly.

Source

do_action( 'edit_user_profile_update', $user_id );

Changelog

Version Description
2.7.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    This example shows how to save a custom field named ‘your_field‘.

    function update_extra_profile_fields($user_id) {
         if ( current_user_can('edit_user',$user_id) )
             update_user_meta($user_id, 'my_custom_field', $_POST['your_field']);
    }
    add_action('edit_user_profile_update', 'update_extra_profile_fields');