Last active
November 13, 2023 03:40
-
-
Save villesiltala/1752401620eb25fcbc65ff7e73fd82d4 to your computer and use it in GitHub Desktop.
Obfuscate WordPress user data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This script obfuscates WordPress user data for defined user roles. | |
*/ | |
global $wpdb; | |
// This example obfuscates all user data for WooCommerce customers. | |
$roles = [ | |
'customer', | |
]; | |
$users = get_users( [ 'role__in' => $roles ] ); | |
foreach ( $users as $idx => $user ) { | |
$user_hash = md5( uniqid( '', true ) ); | |
$random_email = 'anonymous-' . $user_hash . '@somerandomeemail.com'; | |
$name = 'anonymous'; | |
$user_data = [ | |
'user_email' => $random_email, | |
'nickname' => $name . $idx, | |
'display_name' => $name . ' ' . $idx, | |
'user_nicename' => $name . $idx, | |
'first_name' => ucfirst( $name ), | |
'last_name' => $idx, | |
'user_login' => 'anonymous' . $user_hash, | |
]; | |
// Update user data. | |
$wpdb->update( $wpdb->users, $user_data, [ 'ID' => $user->ID ] ); | |
} | |
// Flush the cache after direct database manipulations. | |
wp_cache_flush(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script obfuscates WordPress user data for defined user roles. Making users anonymous might come handy for development environments or other situations where user data should be obfuscated to follow GDPR privacy and data protection requirements.
Easiest way to execute the script is to run it with WP-CLI: