WP-CLI Cheatsheet for site setup

For those who are comfortable with WP-CLI but haven’t yet memorised all the commands here are some of the more common ones I use when developing client sites. The object here is to save time and to avoid do repetitive (and unexciting) tasks in wp-admin. If you aren’t comfortable with WP-CLI and would like to be I suggest starting by reading Up and Running with WP-CLI. This is not to insinuate that the documentation for WP-CLI is lacking (I personally find it very thorough) but in case you are someone who likes to copy and paste your own commands, this should get you started.

To use the cli to install your site

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

wp db create
wp core install

To add and activate a theme from wordpress.org or from a local zip file

wp theme install twentytwelve --activate
wp theme install /srv/home/wp-site-setup/genesis.2.2.3.zip

To Create some default pages

wp post create --post_type=page --post_title='Location' --post_status=publish
wp post create --post_type=page --post_title='About' --post_status=publish
wp post create --post_type=page --post_title='Contact' --post_status=publish
wp post create --post_type=page --post_title='Terms and Conditions' --post_status=publish

You can also use the ‘–prompt’ flag and fill in the arguments as you go along. For example:

wp post create --post_type=page --prompt

will prompt you with the following:

1/4 [<file>]: # this allows you to add content from a file. You would enter the path of the file to be read.
2/4 [--<field>: post_title # this is where you enter the field information for the post the values are those of https://codex.wordpress.org/Function_Reference/wp_insert_post
=<value>]: Location
[--<field>: post_content
=<value>]: --Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
[--<field>: post_type
=<value>]: page
3/4 [--edit] (Y/n): n
4/4 [--porcelain] (Y/n): n # Outputs only the new menu item id.

Another set of handy commands are those concerning the menus.

wp menu create "Primary Menu"
wp menu item add-post primary-menu 3 #this is the post id
wp menu item add-post primary-menu 4 --title="About Us"
wp menu location assign primary-menu primary

Now for some real fun and time savings. Installing plugins using wp-cli is relatively easy. There are three options for the plugin itself; a plugin slug, the path to a local zip file, or URL to a remote zip file.

wp plugin install what-the-file --activate
wp plugin install https://deliciousbrains.com/dl/wp-migrate-db-pro-latest.zip?licence_key=mywpmigratebdprolicencekeywouldgohere&site_url=<WEBSITE_URL_WITH_NO_HTTP>
wp plugin install http://connect.advancedcustomfields.com/index.php?p=pro&a=download&k=myreallylongacflicencekeywouldgohere

When installing a plugin via a local zip file remember that if you are on a server or virtual machine, the path must be on the server. For example if you have a folder on your server named wp-site-setup where you keep several frequently used plugins you might do something like:

wp plugin install /srv/home/wp-site-setup/gravityforms.zip

I also like to add myself a new user and give myself admin permissions:

wp user create kirsten_admin --prompt

Eventually you might want to turn your own setup file into a shell script and become even more efficient. Again, please don’t think that this even approaches the documentation for WP-CLI. There are many more commands that deal with site setup and even more that don’t. If there are any that you find especially useful for setting up a WordPress site, don’t hesitate to comment and I will be happy to add to this list and will probably do so regularly.