Using WordPress ‘profile_update’ PHP action

The profile_update WordPress PHP action fires immediately after an existing user is updated.

Usage

add_action('profile_update', 'your_custom_function', 10, 3);

function your_custom_function($user_id, $old_user_data, $userdata) {
    // your custom code here
}

Parameters

  • $user_id (int) – User ID.
  • $old_user_data (WP_User) – Object containing user’s data prior to update.
  • $userdata (array) – The raw array of data passed to wp_insert_user().

More information

See WordPress Developer Resources: profile_update

Examples

Log user profile updates

Log user profile updates in a file.

add_action('profile_update', 'log_user_profile_updates', 10, 3);

function log_user_profile_updates($user_id, $old_user_data, $userdata) {
    $log_file = fopen("user_updates.log", "a");
    fwrite($log_file, "User ID: " . $user_id . " updated on: " . date("Y-m-d H:i:s") . "\n");
    fclose($log_file);
}

Send email notification on email change

Send an email notification when a user changes their email address.

add_action('profile_update', 'send_email_on_email_change', 10, 3);

function send_email_on_email_change($user_id, $old_user_data, $userdata) {
    if ($old_user_data->user_email != $userdata['user_email']) {
        wp_mail($userdata['user_email'], 'Email Address Changed', 'Your email address has been updated.');
    }
}

Update custom user meta on role change

Update custom user meta when a user’s role is changed.

add_action('profile_update', 'update_custom_meta_on_role_change', 10, 3);

function update_custom_meta_on_role_change($user_id, $old_user_data, $userdata) {
    if (isset($userdata['role']) && $old_user_data->roles[0] != $userdata['role']) {
        update_user_meta($user_id, 'previous_role', $old_user_data->roles[0]);
    }
}

Set user’s display name to their first and last name

Update the user’s display name to their first and last name after profile update.

add_action('profile_update', 'set_display_name_to_first_last', 10, 3);

function set_display_name_to_first_last($user_id, $old_user_data, $userdata) {
    $first_name = $userdata['first_name'];
    $last_name = $userdata['last_name'];
    $display_name = trim($first_name . ' ' . $last_name);
    if (!empty($display_name)) {
        wp_update_user(array('ID' => $user_id, 'display_name' => $display_name));
    }
}

Copy user’s email to a custom field

Copy the user’s email address to a custom field after profile update.

add_action('profile_update', 'copy_email_to_custom_field', 10, 3);

function copy_email_to_custom_field($user_id, $old_user_data, $userdata) {
    if ($old_user_data->user_email != $userdata['user_email']) {
        update_user_meta($user_id, 'email_copy', $userdata['user_email']);
    }
}