Sunday, August 7, 2016

WordPress Multisite - Create user and blog in command line using php

If you have a WordPress Multisite instance and you have to create blogs for your users, using the web interface of WordPress is not a good idea because you have to do it one by one. Here is a solution:

1. Create a csv file contains all the user information.
2. Using php and WordPress's function to create users and blogs.
3. Enjoy

You can start from here. This is a php script I wrote to create a single user, blog, and add that user as an administrator of the blog:



Usage:

1. Place that script inside a subfolder of your WordPress installation, for example:

/var/www/wordpress/your_custom_scripts/create_user_blog.php

2. Run it:

$ php /var/www/wordpress/your_custom_scripts/create_user_blog.php <username> <password> <user's display name> <user's blog path> <site category>

Notes:

1. In the script I used my company-assigned id_number as user's blog path.

2. I also add site_category option to the new blog to categorize all my blogs. You can add one or many different options for you user's blogs.

3. I have to ban WP-CLI for this method because for some reasons, It stops working after I upgraded WordPress to 4.5.3