WordPress on Command Line

Wordpress, the Command Line, and You

“What the hell is a wordpress?” is a question you’re definitely not asking yourself right now if you’ve ever even thought about making a website. Everyone and their mama in the web design world knows about Wordpress. It’s open source, it’s fast and easy, insert-percentage-here of the web runs on it, it’s awesome/terrible/flexible/slow, yadda yadda, etc etc, you’ve heard all of this before.



What about the command line? That’s probably a different story. Far less people know the command line than Wordpress; and probably half of those people that know about it are also intimidated by it. 



Does that group include you? Do you have no problem installing plugins, customizing themes, or managing Wordpress databases, but get weak in the knees and need to run to the restroom at the thought of typing into that black hole box of potential doom?



Don’t be afraid :)

The command line offers a million ways for you to speed up, automate, and generally make your workflow and life easier.

“But I’m a web developer, not a server administrator; why should I learn the command line? Why should I even read the rest of this dumb article?”



I’m glad you asked. The answer is: WP-CLI. 



Short for WordPress CommandLine Interface, WP-CLI has been developed for years, with Github showing its first release back in 2011, but it hasn’t been receiving the attention it deserves.



Here’s a quick list of some of the things you can do with WP-CLI:

Download and install Wordpress

Generate or edit a wp-config file

(Auto)Update one or multiple Wordpress installations

Download, install, and update plugins

Delete themes

Make website+database backups

Debug plugin conflict issues

Build custom plugins

Run custom php with or without loading Wordpress

Run commands remotely to edit Wordpress

Batch Update Serialized wp_options Settings

Delete Wordpress transients

...Basically, anything you can do as an admin on Wordpress in your browser, you can do with WP-CLI, and much much more.

Most web hosts should offer WP-CLI with their Wordpress hosting package. But in case they don’t, here’s how you can install it and get started, using the recommended Phar build! (if you’re using Composer, you can find instructions here )

First, make sure your server has some basic requirements. You’ll need to have:

PHP 5.3.2 or later.

WordPress 3.4 or later.

A Unix-like operating system (OS X, Linux, FreeBSD, Cygwin)

Next, download the WP-CLI.phar file using curl or wget:

curl -O https://raw.githubusercontent.com/WP-CLI/builds/gh-pages/phar/WP-CLI.phar

Test it out by running:

php WP-CLI.phar --info

To be able to just type wp the file needs to be in your PATH and executable. Run:

chmod +x WP-CLI.phar

sudo mv WP-CLI.phar /usr/local/bin/wp

Now try running wp --info

You should get the same output as the first command we tried.

To add bash word completion, run:

wget https://github.com/WP-CLI/WP-CLI/raw/master/utils/wp-completion.bash

Then add to your user’s bash profile (which can be any variation of .bashrc, .profile, .bash_profile, etc):

source /path/to/wp-completion.bash

Don’t forget to change that to your actual path!

Lastly, run:

source ~/.profile to get it completions working

BAM! Wp-cli is now installed* :)

*In order for you to use WP-CLI without issue, the user you used to install WP-CLI must be the user that owns the root directory and subdirectories of where wordpress is installed, or you must be able to run commands as the user that does. Alternatively, you can do everything as root user; WP-CLI will caution against this, but will allow you to run wp commands by adding --allow-root flag to the end of any of your commands. Be careful!

Here’s a quick list of some useful commands:

Download and install the latest version of wordpress. Make sure to change the path to your actual path!

wp core download --path=/var/www/website_rootdirectory

Create a new WP config file. Change db_name and the path to your desired values. After running this you will be prompted to enter the db password you’d like to use.

wp config create --dbname=db_name --dbuser=root --prompt=db_pass --path=/path/to/wp_install

Create your database based on your wp-config file

wp db create

Install Wordpress using your wp-config file and your database

wp core install --url=wpdemo.codestaff.io --title="WP-Cli Demo" --admin_user=admin --admin_password=password --admin_email=codestaff@demo.com

Install a plugin:

wp plugin install plugin_name

You can also search for plugins by replacing “install” with “search” in the above command

Update all your plugins:

wp plugin update --all

Load and execute arbitrary PHP code

wp eval-file /path/to/filename

You can add the flag --skip-wordpress to load and execute without loading wordpress

Activate, deactivate, or check the status of Wordpress’ maintenance mode:

wp maintenance-mode activate

wp maintenance-mode deactivate

wp maintenance-mode status

Import an image and set it to be a post’s featured image. Replace the images path, post id and your desired title

wp media import ~/Downloads/image.png --post_id=123 --title="my post’s featured picture" --featured_image

Troubleshoot a frontend plugin issue/conflict without manually disabling all of them! Run the first three commands separately, then run the final one as a bash loop. It will run through and activate each plugin one at a time; from here you can easily find which plugin is causing you trouble

wp plugin deactivate --all

wp plugin list --status=inactive --field=name

wp plugin list --status=inactive --field=name > pluginlist

while read -r PLUGIN; do

echo "Activating ${PLUGIN}"

wp plugin activate ${PLUGIN}

read -p "Press Enter to continue" </dev/tty

done < pluginlist

That’s just a quick list of some useful commands, we’ll compile a comprehensive list for you to play with soon. You can also check out WP-cli documentation here. What can YOU do with WP-CLI?