Using WordPress ‘edit_user_profile_update’ PHP action

The edit_user_profile_update WordPress PHP action fires before the page loads on the ‘Edit User’ screen.

Usage

add_action('edit_user_profile_update', 'my_custom_function');
function my_custom_function($user_id) {
    // your custom code here
}

Parameters

  • $user_id (int) – The user ID.

More information

See WordPress Developer Resources: edit_user_profile_update

Examples

Update user meta

Update a custom user meta field when editing a user profile.

add_action('edit_user_profile_update', 'update_user_custom_meta');
function update_user_custom_meta($user_id) {
    if (isset($_POST['custom_meta'])) {
        update_user_meta($user_id, 'custom_meta', sanitize_text_field($_POST['custom_meta']));
    }
}

Send a notification email

Send a notification email to the administrator when a user profile is updated.

add_action('edit_user_profile_update', 'send_notification_email');
function send_notification_email($user_id) {
    $user = get_userdata($user_id);
    $admin_email = get_option('admin_email');
    wp_mail($admin_email, 'User Profile Updated', 'User ' . $user->user_login . ' has updated their profile.');
}

Log user profile updates

Log user profile updates in a custom log file.

add_action('edit_user_profile_update', 'log_user_profile_update');
function log_user_profile_update($user_id) {
    $log_message = 'User ID ' . $user_id . ' profile has been updated. Time: ' . date('Y-m-d H:i:s');
    error_log($log_message, 3, '/path/to/your/custom-log.log');
}

Check user role

Check if the user being updated has a specific role and perform an action.

add_action('edit_user_profile_update', 'check_user_role');
function check_user_role($user_id) {
    $user = get_userdata($user_id);
    if (in_array('editor', $user->roles)) {
        // Perform an action if the user has the 'editor' role
    }
}

Remove user role

Remove a specific role from a user when their profile is updated.

add_action('edit_user_profile_update', 'remove_user_role');
function remove_user_role($user_id) {
    $user = new WP_User($user_id);
    if ($user->has_cap('editor')) {
        $user->remove_role('editor');
    }
}