How to anonymize the WordPress users on your local or staging site

You never want to accidentally send emails to users from your local or staging version of your site, especially if you run an e-commerce site with a bunch of customer data.

Fortunately it’s pretty easy to change all of the emails addresses in your users table to something with a fake domain by running this MySQL query:

update wp_users set user_email = concat(substring_index(user_email, '@', 1), FLOOR(rand() * 90000 + 10000), '@localhost.dev')

This will update the email domains to @localhost.dev and adds a random 5 digit number in case you have multiple email addresses with the same first part before the @.

You can repeat for any other columns in your database that might have email addresses, such as this one for Easy Digital Downloads:

update wp_edd_customers set email = concat(substring_index(email, '@', 1), FLOOR(rand() * 90000 + 10000), '@localhost.dev')

In addition you can run your WordPress email through something like Mailhog using a plugin. I don’t just recommend that because of the name 🙂