Advertisement
  1. Code
  2. WordPress
Code

Command-Line WordPress

by
Difficulty:BeginnerLength:ShortLanguages:
Final 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:

Check it worked with:

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

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:

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.:

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:

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

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:

Installing a Plugin

To install a plugin, you can simply run:

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

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

Multisite Installations

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

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:

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

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.

Advertisement
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.