I manage a WordPress Multi-user environment and needed to bulk add LDAP users (which is already quite easy) and also to bulk create blogs for users – which I couldn’t find an easy method for. I started exploring how I could script the creation of this when I happened upon WP-CLI.
yum install git
Then to install WP-CLI:
curl https://raw.github.com/wp-cli/wp-cli.github.com/master/installer.sh | bash
You should see the following output:
Then you should manually add the following to your .bash_profile so that the ‘wp’ command can be found without entering the full path to it.
export PATH=/root/.wp-cli/bin:$PATH
Go to the directory that WordPress is installed in and run:
wp core version
You should see the currently installed version output.
I run a WordPress Multi-user environment so wp-cli would be of most use when creating bulk sites. A single site can be created as follows:
wp site create --slug="mynewsite" --title="A Bloggers Dream" --email="jonny@domain.uk"
That would create a site http://blogs.domain.uk/mynewsite with the user jonny@domain.uk as the administrator. Dozens of sites can then be created be feeding a list into a shell script.
What I need next is the ability to add a particular (supervisor) user as an administrator to a bulk list of blogs. I can do it with built in users e.g.
wp user add-role "admin" administrator --url="blogs.domain.uk/student72/"
But this does not appear to work with my LDAP linked users 🙁
So I manually created a script to output the SQL to produce output for each site as follows;
INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES (175, 'wp_222_user_level', '10');
INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES (175, 'wp_222_capabilities', 'a:1:{s:13:"administrator";b:1;}');
The script:
#!/bin/bash
for SITENUM in `seq 222 245`;do
echo "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES (175, 'wp_"$SITENUM"_user_level', '10');"
echo "INSERT INTO wp_usermeta (user_id,meta_key,meta_value) VALUES (175, 'wp_"$SITENUM"_capabilities', 'a:1:{s:13:\"administrator\";b:1;}');"
done