#
What is WP-CLI and what can it do for you?
#
So what is WP-CLI?
WP-CLI or the WordPress Command Line Interface is a PHP based tool that allows you to manage your WordPress site via SSH (Secure Shell) interface.
In simpler terms, it is the back-door to your website. WP-Admin is the GUI or Graphical User interface gives you something nice to look at, with buttons and colours that can be accessed via a web page. WP-CLI drops the GUI and works with simple commands, providing more control and speed.
#
Why should you use WP-CLI?
This will depend on whether you manage one site or multiple sites but there is always benefit to using WP-CLI. When I am doing regular maintenance on a site or inspecting a client site for the first time there are several commands I always run.
wp plugin status
wp theme status
wp core check-update
#
Fundamentals of a WP-CLI command
wp core update --version=4.9.1 --force
WP-CLI install page has details on how to add tab completions, so you just hit tab and get a list of available commands, sub-commands and options. https://wp-cli.org
#
Let's dig deeper and see what we can do!
wp user list
wp user update 2 --user_pass=Its@Secret!
wp user update 2 --user_pass=Its@Secret!
wp user delete iamnewuser
Also a number of maintenance tools for DB as well but be very careful running them. Best rule if you are not sure, don't run them.
wp db export pluginupdate.sql
wp db import pluginupdate.sql
wp search-replace "//yoursite.com" "//staging.yoursite.com" --dry-run
wp db repair
wp db optimize
wp db drop
wp post list --post_type=page --fields=post_title,post_statuses
wp post list --post_type=post --posts_per_page=5 --format=csv
wp post list --post_type=post --posts_per_page=5 --format=csv > posts.csv
#
Building and Scaffolding Sites
Here are some more detailed commands for building and setting up your sites. You will need a database in place before you begin.
wp core wp core download
wp core config --dbname=mrwilde_database --dbuser=mrwilde_user --dbpass=mrwilde_password --dbhost=localhost --dbprefix=mrwilde_
wp core install --url=mrwilde.com --title="Mr Wilde's Website" --admin_user=admin_user --admin_password=admin_password --admin_email="robert@mrwilde.com"
wp plugin install query-monitor developer php-compatibility-checker theme-checker --activate
wp scaffold child-theme mrwilde-theme --parent_theme=twentyseventeen --theme_name='MrWilde New Theme' --author='Robert Wilde' --author_uri=https://mrwilde.com --theme_uri=https://mrwilde.com --activate
#
Advanced commands and Piping
Here a some more advanced commands that you can use for testing and fault-finding. I would highly advise you don't use these on a production server as they will change your site and possibly prevent it from being accessed via a web page.
These commands are for development & staging environments only and make sure you do a wp db export database-name.sql first as insurance.
wp post list --post_type=attachment --post_mime_type=application/pdf --format=ids | xargs wp media regenerate
wp post delete lets you easily delete all posts.
wp post delete wp post delete $(wp post list --post_type='page' --format=ids) // run a commands remotely on a site wp user list --ssh=robert@mrwilde.com/var/www
wp core download --version=nightly --force
That is only a small sample of commands for WP-CLI. Here are some links for beginners and advanced users as well as a few posts on how to install WP-CLI on your host.
- Home of WP_CLI
- Beginners Guide to WP-CLI
- WP-CLI Tips by John Blackbourn (Creator of Query-Monitor)
- WP-CLI – Advanced WordPress Management
- Other External Resources