WP-CLI is the perfect tool to manage WordPress from the terminal. You can use it to install themes and plug-ins, update resources, and manage users. This guide will walk you through the most useful commands and will show you how to use them in practice.
Installing WP-CLI on server
WP-CLI is most often run remotely via SSH. To install the CLI, run these commands on your server:
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar $ chmod +x wp-cli.phar $ mv wp-cli.phar /usr/local/bin/wp
To check if the installation was successful, run
$ wp --info
Now you can run WordPress commands. Let's make a tour of the most useful ones.
Follow these steps to install WP from the command line level:
- Download installation files
$ wp core download
$ wp config create --dbname=DB_NAME --dbuser=DB_USER --dbpass=DB_PASS
- Install WordPress
$ wp core install --url=wordpress.site --title="WordPress site title" --admin_user=admin_login --admin_password=admin_pass --email@example.com
If you don't want to enter the password in a direct way, you can use the
$ wp core install --url=wordpress.site --title="WordPress site title" --admin_user=admin_login --firstname.lastname@example.org --prompt=admin_password < admin_password.txt
Updating WP Core
You know how to install WordPress. This command will help you make sure it's up-to-date:
$ wp core update
WL-CLI lets you install any WordPress plug-in:
$ wp plugin install hello-dolly
And activate it:
$ wp plugin install hello-dolly --activate
Or do it in two separate steps:
$ wp plugin install hello-dolly $ wp plugin activate hello-dolly
If you no longer need a plug-in, you can turn it off using
$ wp plugin deactivate hello-dolly
Or turn it off and delete entirely:
$ wp plugin uninstall hello-dolly --deactivate
You can check what plug-ins you have installed with the
$ wp plugin list
WP-CLI lets you easily update existing plug-ins:
$ wp plugin update hello-dolly
The command comes with three useful parameters:
--dry-run parameter will let you list the plug-ins that require and update without actually running the process:
$ wp plugin update --all --dry-run
--all will force an update on all plug-ins:
$ wp plugin update --all
--minor, on the other hand, will only allow lesser updates, for example 1.0 to 1.1, but not 1.0 to 2.0.
$ wp plugin update --all --minor
Theme management is basically the same as managing plug-ins – you just need replace
To install and activate a theme, run
$ wp theme install twentytwenty $ wp theme activate twentytwenty
Or use a single command:
$ wp install twentytwenty --activate
To delete a theme, run
$ wp theme delete twentytwenty
To update all themes at once, use
$ wp theme update --all
And to list them, run
$ wp theme list
You can also add
--all parameters. Be aware, though, that
--minor doesn't work when updating themes.
WP-CLI also offers a couple of commands to manage you co-workers. To display the list of users added to your WP accountr, run
$ wp user list --all
To restrict the list to admins only, add the
$ wp user list --role=administrator
To add a user with a given role, enter the following:
$ wp user create buddy email@example.com --role=author --user_pass=some-password
Here are the roles you can assign:
subscriber. You can read about the roles here.
To edit an existing user, use
$ wp user update buddy --user-pass=new-password --first_name="new name"
Managing images and media
The commands below will come in handy during migration or rebuilding a website from scratch.
wp media regenerate command lets you regenerate images on your website:
$ wp media regenerate --yes
If you added new media dimensions to your theme, you can generate only the missing image sizes by adding a parameter to the command:
$ wp media regenerate --yes --only-missing
Or if you want to generate a specific size:
$ wp media regenerate --yes --image_size=new_image_size
Another very useful command is
wp media import. It allows you to import images from the selected directory on the server:
$ wp media import ~/Pictures/**\/*.jpg
Or you can import an image from an external server:
$ wp media import https://example.com/images/test.jpg
You can also import the image and immediately set it up as featured:
$ wp media import https://example.com/images/test.jpg --featured_image --post_id=123
Search and replace in database
Whenever migrating something in WordPress,
wp search-replace proves indispensable. Besides using it to find and replace text, it copes really well with serialized data, a format often used by WordPress to store data.
For example, to change the domain, just run
$ wp search-replace 'http://example.test' 'http://example.com'
The command above will replace the domain in all default WordPress tables. However, if you'd like to replace it only in tables of a specific prefix, you need to add a parameter:
$ wp search-replace 'http://example.test' 'http://example.com' --all-tables-with-prefix
If you want to update all tables in the database, use just --all-tables.
And finally, what if we owant to replace the domain in
wp_posts only? Use this:
$ wp search-replace 'http://example.test' 'http://example.com' wp_posts
The operations described here are just a fragment for what WP-CLI can help you achieve. The full list of commands is available here. Adding Buddy will let you automate many of them and save loads of time.
For example, you can configure a pipeline that will perform a full back of your WP instance and upload it on the backup server every day:
Stay tuned for more on WordPress optimization with Buddy CI/CD!