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:
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check it worked with:
php wp-cli.phar --info
Now to set this as just a wp
command available anywhere, run the following:
chmod +x wp-cli.phar 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
:
$ 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.:
$ wp help cli DESCRIPTION Get information about WP-CLI itself. SYNOPSIS wp cli <command> SUBCOMMANDS check-update Check for update via Github API. Returns the available versions if there are updates, or empty if no update available. cmd-dump Dump the list of installed commands, as JSON. completions Generate tab completion strings. info Print various data about the CLI environment. param-dump Dump the list of global parameters, as JSON or in var_export format. update Fetch most recent update matching the requirements. Returns the available versions if there are updates, or empty if no update available. version Print WP-CLI version.
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:
$ wp db export
This will give you an uncompressed backup and a .SQL file which you can then create a tarball from as so:
$ 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:
$ mysql -u<username> -p<password> wordpress < backup.sql
Installing a Plugin
To install a plugin, you can simply run:
$ wp plugin install <plugin name>
You can see what plugins are installed with wp plugin list
:
$ wp plugin list +-------------+--------+-----------+---------+ | name | status | update | version | +-------------+--------+-----------+---------+ | jetpack | active | none | 3.7.2 | | woocommerce | active | available | 2.4.7 | +-------------+--------+-----------+---------+
You can turn these plugins on and off with the activate
or deactivate
arguments as so:
$ wp plugin activate jetpack 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:
$ wp theme status --url=localhost/wp/test # For a subdomain just specify the url $ 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
:
url: test.example.com
You will now be able to omit the --url
argument from your wp
commands, e.g.:
$ 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.
Comments