Automating site creation with WP-CLI
Did you know that there is a command-line tool for WordPress automation called wp-cli? If not, you are not the only one. Actually, a few people are aware that you can use it to automate specific repetitive tasks with WordPress. No need to say, this can save you a lot of time and effort in the process. For example, by using this tool you can delete thousands of spam comments in mere seconds, create .pot
translation files, install and activate plugins, themes, create and manage users, content, etc. And the best part of it is that you do not even need to login to the admin dashboard! Everything can be done directly from the command line. This might not make a huge difference if you maintain just one WordPress site, but guess what happens if you have to maintain hundreds of websites and you have limited time to go through every each of them and do the changes? In such situation, wp-cli comes to the rescue!
Prerequisites
In order to make a WordPress website with wp-cli, you need to have wp-cli installed on your web server. Check with your hosting provider if it has wp-cli installed. Some shared hosting providers like HostArmada have wp-cli preinstalled on their systems, which means you can start using it right away. In this post, I will assume that you have ssh access to Ubuntu web server as a root user and the user is running LAMP (you are allowed to install any type of additional software on your server). If you already have wp-cli installed, you can skip below section.
Installing WP-CLI
You can follow the official WordPress documentation on how to install wp-cli. Once you have authenticated with ssh to your web server, write the below command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
This will download the Phar build on your server, make it executable and place it in your PATH. Next, check if the script works:
php wp-cli.phar --info
If you want to just type wp
instead of php wp-cli.phar
, you need to make the file executable and move it to somewhere in your PATH. Run the following commands:
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Finally, type wp --info
to make sure the above configuration works.
Creating and setting up a WordPress website with WP-CLI
Once you have wp-cli installed on your web server, automating website creation is a fairly straightforward process. First, go to your script directory (in Ubuntu it is /usr/local/bin ) and create a bash script called create-wp-site.sh:
nano create-wp-site.sh
This will use the nano editor to create an empty file create-wp-site.sh
. Now, lets add code into our bash script:
#!/bin/bash if ! command -v wp &> /dev/null; then echo "WP-CLI is not installed. Please install it first: https://wp-cli.org/" exit 1 fi # Set up variables WEBSITE="http://myawesomewebsite.com" WP_PATH="/www/wwwroot" WP_TITLE="My WordPress Site" DB_NAME="" DB_USER="" DB_PASS="" DB_HOST="localhost" DB_PREFIX="wp" WP_USER="" WP_PASS="" WP_EMAIL="[email protected]" WP_THEME="pliska" domain=$(echo "$WEBSITE" | awk -F[/:] '{print $4}') # Extract domain from URL wp_path="$WP_PATH/$domain" # Construct website path # Download WordPress wp core download --path="$WP_PATH/$domain" --locale=en_US # Go to the correct folder and set up the rest cd "$wp_path" || exit # Create wp-config.php manually wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASS" --dbhost="$DB_HOST" --dbprefix="$DB_PREFIX" # Install WordPress wp core install --url="$WEBSITE" --title="$WP_TITLE" --admin_user="$WP_USER" --admin_password="$WP_PASS" --admin_email="$WP_EMAIL" # Install and activate theme wp theme install "$WP_THEME" --activate echo "WordPress installation completed for $WEBSITE. Visit $WEBSITE to access your site."
Before saving the script, make sure you have updated the variables at the top of the file with your own data. Also make sure you have created a database, database user and password beforehand. When you are happy with the changes, save the file (with nano you can do ctrl + X and then press “y”). Finally, make the file executable by running:
chmod +x create-wp-site.sh
That was all! You have just learnt how to create a WordPress website with wp-cli. As you can see, wp-cli is a very powerful tool with huge potential. It can help you automate a lot of boring and repetitive tasks, as well as fine-tune your WordPress development skills. If you have repetitive WordPress tasks in your agenda but you haven’t implemented wp-cli yet, I highly recommend that you start doing so as soon as possible.