Advertisement
  1. Code
  2. WordPress

Command-Line WordPress

Scroll to top
4 min read
Final product imageFinal product imageFinal product image
What You'll Be Creating

Developers and users of WordPress are all too familiar with the /wp-admin administration panel and how it works.

This guide is for users and admin alike. I’ve heard them many times complaining (system admins in particular) about WordPress, and how due to the plethora of configuration options in the WordPress admin panel it can often become fiddly and confusing finding where to click or remembering where a certain feature is residing. As it is all down to the plugin creators, there is no centralised way to interact with WordPress as an actual direct command interface.

Well, those days are truly over because WordPress has a command-line tool, and it’s a serious timesaver!

Get the Tool

Download WP-CLI with the following at the terminal prompt:

1
2
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Check it worked with:

1
2
php wp-cli.phar --info

Now to set this as just a wp command available anywhere, run the following:

1
2
chmod +x wp-cli.phar
3
sudo mv wp-cli.phar /usr/local/bin/wp

This will require your root password for completion of the sudo command.

For the official guide, go to the WP-CLI website to get more detailed instructions and information about the WP-CLI project (such as MAMP configuration issues and other useful support topics)

Usage

To use the WP-CLI tool, just cd to your WordPress installation directory and run wp:

1
2
$ wp

Working with the wp tool is very self-explanatory. You can perform any of the functions from the browser-based tool now on the command line. Blog creation, code execution, plugin support—it’s all here.

To learn more about the functionality of wp, just run wp help <command>, e.g.:

1
2
$ wp help cli
3
DESCRIPTION
4
5
  Get information about WP-CLI itself.
6
7
SYNOPSIS
8
9
  wp cli 
10
11
SUBCOMMANDS
12
13
  check-update      Check for update via Github API. Returns the available versions if there are updates, or empty if no update available.
14
  cmd-dump          Dump the list of installed commands, as JSON.
15
  completions       Generate tab completion strings.
16
  info              Print various data about the CLI environment.
17
  param-dump        Dump the list of global parameters, as JSON or in var_export format.
18
  update            Fetch most recent update matching the requirements. Returns the available versions if there are updates, or empty if no update available.
19
  version           Print WP-CLI version.
20
</command>

From here, you can perform an update of WordPress with update or just check if one is needed with check-update.

Backing Up

To back up your WordPress with the CLI, just run:

1
2
$ wp db export

This will give you an uncompressed backup and a .SQL file which you can then create a tarball from as so:

1
2
$ tar -vczf yourbackupfilename.gz .

Restoring From a Backup

Set the webroot of your server to the directory created by the wp db export command and import the .SQL with mysql as so:

1
2
$ mysql -u<username> -p<password> wordpress < backup.sql

Installing a Plugin

To install a plugin, you can simply run:

1
2
$ wp plugin install <plugin name>

You can see what plugins are installed with wp plugin list:

1
2
$ wp plugin list
3
+-------------+--------+-----------+---------+
4
| name        | status | update    | version |
5
+-------------+--------+-----------+---------+
6
| jetpack     | active | none      | 3.7.2   |
7
| woocommerce | active | available | 2.4.7   |
8
+-------------+--------+-----------+---------+

You can turn these plugins on and off with the activate or deactivate arguments as so:

1
2
$ wp plugin activate jetpack
3
Success: Plugin 'jetpack' activated.

Multisite Installations

To make your wp work with a multisite WordPress installation, you just need to pass the --url argument as so:

1
2
$ wp theme status --url=localhost/wp/test
3
# For a subdomain just specify the url 
4
$ wp theme status --url=subdomain.my-wordpress-site.com

If you want wp to remember your --url configuration, you can specify it in a wp-cli.yml, which must be located inside your WordPress root.

Add the following to your wp-cli.yml:

1
2
url: test.example.com

You will now be able to omit the --url argument from your wp commands, e.g.:

1
2
$ wp theme status

This will read the configuration from the wp-cli.yml and pass the --url test.example.com for you.

Conclusion

To check for updates, back up your WordPress, add or remove plugins, or change themes, you can do it all now without leaving the command prompt. This makes wp-cli a very powerful tool indeed.

For more information on expanding the wp-cli tool even further, continue your reading in the WP-CLI manual.</command>

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.