Up and running with WP-CLI

This isn’t a tutorial about installing WP-CLI but if you want one I recommend this one.

Let’s compare two ways of downloading WordPress and creating the database.

Without WP-CLI

This is the fastest way I’ve managed to install WordPress on my local server before WP-CLI:

1. Create the database
mysql -u [username] -p[password] -e "create database [databasename];"

Please note that is not a typo. There is a space between the -u and the username but none between the -p and the password. So if your password is “root” you are essentially writing -proot.

You should now have a new database created.

2. Install the WordPress core files

either

wget http://wordpress.org/latest.tar.gz

or

curl -O https://wordpress.org/latest.tar.gz

expand using

tar -xzvf latest.tar.gz

move one level up

mv wordpress/* .

tidy up

rmdir wordpress/ ; rm latest.tar.gz
3. Set up wp-config.php

The following steps are not strictly necessary if you are going to just open it in the browser and enter the information that way, but in case you just refuse to leave the command line:

create wp-config.php

cp wp-config-sample.php wp-config.php

now add the appropriate settings to wp-config.php

nano wp-config.php

Open in browser and you should be good to go. This is all based largely on an article by Neil Gee referenced below.

With WP-CLI

wp core download
wp core config --dbname=mydbname --dbuser=mydbusername --dbpass=mydbpassword --extra-php
> define( 'WP_DEBUG', true );
> define( 'WP_DEBUG_LOG', true );

wp db create
wp core install

Now I know I mixed up the order using WP-CLI but the command wp db create creates the database based on the setting you added in wp core config. This is purely a personal preference on my part. Anyway I think you’ll agree that it is faster with WP-CLI.

Another task that I find to be much easier using WP-CLI is quickly creating some default pages or posts. I have several snippets set up to do this but you can also write a bash script if that’s your thing.

For this all you need to do is:

wp post create --post_type=page --post_title='About' --post_status=publish

Truth be told it doesn’t take ages to set up a WordPress installation but those 5 minutes can add up. WP-CLI provides a wealth of commands that can really speed up your workflow. The ones mentioned here are only the tip of the iceberg but are enough to give you a taste for this efficient tool.

I’d love to hear about any great shortcuts anyone has come across when using WP-CLI. As always, please let me know if there are any errors or typos here.