Using WordPress ‘deleted_usermeta’ PHP action

The deleted_usermeta WordPress PHP action is triggered after a user’s metadata is deleted from the database.

Usage

add_action('deleted_usermeta', 'your_custom_function');

function your_custom_function() {
    // your custom code here
}

Parameters

  • No parameters

More information

See WordPress Developer Resources: deleted_usermeta

Examples

Logging user metadata deletion

Log user metadata deletion in a custom log file.

add_action('deleted_usermeta', 'log_usermeta_deletion');

function log_usermeta_deletion($meta_ids) {
    // Get the usermeta data from meta_ids
    $usermeta_data = get_metadata_by_mid('user', $meta_ids[0]);

    // Prepare log message
    $log_message = sprintf(
        'User ID %d - Meta Key "%s" has been deleted.',
        $usermeta_data->user_id,
        $usermeta_data->meta_key
    );

    // Log the message
    error_log($log_message);
}

Send notification after specific metadata is deleted

Send an email notification to the admin when a user’s specific metadata is deleted.

add_action('deleted_usermeta', 'notify_admin_on_meta_deletion', 10, 2);

function notify_admin_on_meta_deletion($meta_ids, $user_id) {
    $deleted_key = 'special_user_meta_key';

    foreach ($meta_ids as $meta_id) {
        $usermeta_data = get_metadata_by_mid('user', $meta_id);

        if ($usermeta_data->meta_key === $deleted_key) {
            $admin_email = get_option('admin_email');
            $subject = 'User Meta Deleted';
            $message = "The '{$deleted_key}' user meta has been deleted for user ID {$user_id}.";

            wp_mail($admin_email, $subject, $message);
            break;
        }
    }
}

Update user role when specific metadata is deleted

Change the user role when a specific metadata is deleted.

add_action('deleted_usermeta', 'update_user_role_on_meta_deletion', 10, 2);

function update_user_role_on_meta_deletion($meta_ids, $user_id) {
    $deleted_key = 'change_user_role_meta';

    foreach ($meta_ids as $meta_id) {
        $usermeta_data = get_metadata_by_mid('user', $meta_id);

        if ($usermeta_data->meta_key === $deleted_key) {
            $user = get_userdata($user_id);
            $user->set_role('new_role_name');
            break;
        }
    }
}

Delete related user metadata when a specific metadata is deleted.

add_action('deleted_usermeta', 'delete_related_user_meta', 10, 2);

function delete_related_user_meta($meta_ids, $user_id) {
    $deleted_key = 'primary_user_meta_key';
    $related_key = 'related_user_meta_key';

    foreach ($meta_ids as $meta_id) {
        $usermeta_data = get_metadata_by_mid('user', $meta_id);

        if ($usermeta_data->meta_key === $deleted_key) {
            delete_user_meta($user_id, $related_key);
            break;
        }
    }
}

Perform cleanup after user metadata is deleted

Clean up data in a custom table when a specific user metadata is deleted.

add_action('deleted_usermeta', 'cleanup_custom_table_on_meta_deletion', 10, 2);

function cleanup_custom_table_on_meta_deletion($meta_ids, $user_id) {
    global $wpdb;
    $deleted_key = 'cleanup_related_meta_key';
    $custom_table_name = $wpdb->prefix . 'custom_table_name';

    foreach ($meta_ids as $meta_id) {
        $usermeta_data = get_metadata_by_mid('user', $meta_id);

        if ($usermeta_data->meta_key === $deleted_key) {
            $wpdb->delete($custom_table_name, array('user_id' => $user_id));
            break;
        }
    }
}