钩子文档

wp_update_user

💡 云策文档标注

概述

本文档介绍了 WordPress 中的 wp_update_user 动作钩子,它在用户信息更新且邮件发送后触发,允许开发者添加自定义功能。

关键要点

  • wp_update_user 是一个动作钩子,在用户更新操作完成后执行。
  • 钩子传递三个参数:$user_id(用户ID)、$userdata(更新后的用户数据数组)和 $userdata_raw(原始未编辑的用户数据数组)。
  • 开发者可以使用 add_action 函数将自定义函数挂接到此钩子,以在用户更新时执行特定代码。
  • 此钩子自 WordPress 6.3.0 版本引入。

代码示例

// 定义在 wp_update_user 动作触发时执行的函数
function custom_user_update_action($user_id, $userdata, $userdata_raw) {
    // 在此添加自定义代码
    // 例如,记录用户ID和更新的邮箱地址
    error_log('User ID ' . $user_id . ' updated their email to: ' . $userdata['user_email']);
}

// 将自定义函数挂接到 wp_update_user 动作
add_action('wp_update_user', 'custom_user_update_action', 10, 3);

// 模拟用户信息更新(实际环境中由 WordPress 自动触发)
$user_id = 123;
$updated_userdata = array(
    'user_email' => 'newemail@example.com',
    // 其他更新的用户数据字段...
);

// 使用模拟更新触发 wp_update_user 动作
do_action('wp_update_user', $user_id, $updated_userdata, $updated_userdata);

注意事项

  • 在实际 WordPress 环境中,wp_update_user 动作通常由系统自动触发(如通过 wp_update_user() 函数),开发者无需手动调用 do_action。
  • 自定义函数应正确处理参数,避免影响核心功能。
  • 代码示例仅用于演示,实际使用时需根据需求调整日志记录或其他操作。

📄 原文内容

Fires after the user has been updated and emails have been sent.

Parameters

$user_idint
The ID of the user that was just updated.
$userdataarray
The array of user data that was updated.
$userdata_rawarray
The unedited array of user data that was updated.

Source

do_action( 'wp_update_user', $user_id, $userdata, $userdata_raw );

Changelog

Version Description
6.3.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Here’s an example code snippet to demonstrate the usage of the do_action function with the 'wp_update_user' action in a WordPress context:

    // Define a function that will be executed when 'wp_update_user' action is triggered
    function custom_user_update_action($user_id, $userdata, $userdata_raw) {
        // You can add your custom code here
        // For example, let's log the user ID and updated email address
        error_log('User ID ' . $user_id . ' updated their email to: ' . $userdata['user_email']);
    }
    
    // Hook the custom function to the 'wp_update_user' action
    add_action('wp_update_user', 'custom_user_update_action', 10, 3);
    
    // Simulate an update to user information (this would be an actual user update)
    $user_id = 123;
    $updated_userdata = array(
        'user_email' => 'newemail@example.com',
        // Other updated user data fields go here...
    );
    
    // Trigger the 'wp_update_user' action with the simulated update
    do_action('wp_update_user', $user_id, $updated_userdata, $updated_userdata);

    Explanation of the code:

    1. We define a custom function custom_user_update_action that takes three parameters: $user_id, $userdata, and $userdata_raw. Inside this function, you can write any custom code you want to execute when a user’s information is updated. In this example, we’re just logging the user ID and the updated email address.
    2. We use the add_action function to attach our custom function custom_user_update_action to the 'wp_update_user' action. This means that whenever the ‘wp_update_user’ action is triggered, our custom function will be called.
    3. In this simulated example, we set the $user_id to 123 (which would be the actual user ID) and create an array $updated_userdata containing the updated user data. This could include changes to the user’s email, display name, or any other relevant fields.
    4. Finally, we use the do_action function to manually trigger the 'wp_update_user' action with the provided parameters. This simulates an update to the user’s information and will cause our custom function to be executed.

    Remember that in a real WordPress environment, these actions and hooks are automatically triggered by various processes within WordPress, and developers can use them to add their own custom functionality at specific points in the system’s execution.