Use WP-CLI to generate random users

05 Feb 2016

I’ve been working on WP User Manager for nearly a year now ( first code commit was February 17 2015 ). During development I quickly started running into those things that add up to a lot of time wasted.

Creating, updating and deleting multiple test users in WordPress is a very slow process. In the past, during my development test phases I just used the WP UI to create and update users because I couldn’t find a better way to do it.

Inspired by a recent talk about WP-CLI at the “A Day of Rest” conference in London last week, I decided to build a custom command to help me generate random users within WordPress and speed things up.

Generate users

The generate subcommand can generate up to 1000 random users by default. The random data is retrieved from a json file within the plugin’s folder. The plugin will take care of generating username, password, email, first name and last name.

Mock data has been created with Mockaroo. If you need to generate more than 1000 users, use mockaroo.com to create your own json file and replace the existing one within the plugin’s folder. It’s important that you include all the default fields within your json file.

Use the generate subcommand followed by the number of users you need.

wp usergen generate 20

A progress bar will then appear showing the status of the generation.

Delete all users

The plugin also has a “purge” subcommand that when used will delete all users on the site excepts administrators.

The purge subcommand does not have any other parameter. Here’s an example:

wp usergen purge

A confirmation dialog will appear, once confirmed, multiple progress bars will appear for each user role detected within the site.

I hope you'll find this useful. If you have any suggestions, get in touch.