1. Home
  2. Developer tools
  3. How do I use WP-CLI?

How do I use WP-CLI?

WP-CLI is available on all our web hosting accounts, Managed Servers and Agency services. It lets you manage WordPress installations via terminal/SSH.

To be able to use WP-CLI, you must first access your account via SSH. We’ve written instructions here describing how you get started with SSH.

Once you’ve set everything up so you can access your account via SSH, you can continue reading this guide where we’ll go through a number of useful WP-CLI features. You may also use the terminal available via cPanel for this.

WP-CLI has many more features than we can cover in this article. You find them all on its official website.

To be able to use WP-CLI

To be able to use WP-CLI, you must navigate to the document root of the WordPress installation. If the installation is located in the public_html folder, you type cd public_html directly after logging on to you account via SSH to enter that folder. If the installation resides in another folder, you will need to type cd path/to/your/installation instead (using the correct path).
You can use the command pwd to see the path to the folder where you are currently, and the command ls -la to list the contents of your current folder.

Basic commands

Once you’ve navigated to the document root of your WordPress installation (as described above), use any of the following commands to get information about the installation:

See the version of WordPress that is being used:

wp core version

Check if there are any updates available for the WordPress core:

wp core check-update

See status for all installed plugins, which ones are activated, and if there are any available updates:

wp plugin status

In the same manner as for plugins, you can check the status for all installed themes:

wp theme status

Update WordPress core, plugins, and themes

After navigating to the document root of your WordPress installation (as described above), you can use WP-CLI to update the WordPress core to the latest version. To do this, run these commands:

wp core update
wp core update-db

You can also use WP-CLI to update installed plugins to the latest version. If you want to update all plugins on your WordPress installation at once, use this command:

wp plugin update --all

If you only want to update a specific plugin, instead use:

wp plugin update akismet

In this example, the plugin Akismet will be updated, swap out akismet to match the name of the plugin you want to update.

In the same manner, you can update themes. Using this command, you can update all currently installed themes:

wp theme update --all

If you want to update a specific theme, run the following (instead of using --all):

wp theme update twentyeleven

Remember to swap out twentyeleven in the example for the name of the theme you wish to update.

Replace the site url used by WordPress

To change the domain a WordPress installation uses, e.g. swapping from test1.se to test2.se, can be quite a laborious process since the original domain (test1.se) will exist in a number of places all over the database used by the installation. To ensure that WordPress works properly, they all need to be replaced with the new domain, a task that can require vast amounts of time, was the job to be done manually. However, WP-CLI has a solution for this, making the process way faster!

Do not forget to navigate to the document root of the WordPress installation where you want to change site url. Do this before running the commands below. We describe above how you navigate to the correct location.

WP-CLI has a so called “search and replace” feature, which can go through your database, looking for the original domain (test1.se) and replacing them with the new domain (test2.se). To use it, simply run:

wp search-replace '://test1.se' '://test2.se'

If you execute this command, WP-CLI will replace all occurrences of test1.se in the database with test2.se. If you add --dry-run at the end of the command, the feature will be run but not perform any changes. You will, however, get a report of the changes that could be done:

wp search-replace '://test1.se' '://test2.se' --dry-run

Remember to swap out test1.se and test2.se for the correct domains for your case.

Change from http to https in URLs

If you recently switched from http to https on your website, but browsers still warn that the website is not secure due to mixed content, this may be because some resources (such as images) still load via http.

You can then use the “search and replace” feature described above to fix this. Follow the instructions above, but instead of replacing ://test1.se with ://test2.se, make sure that you search for http://test2.se and replace it with https://test2.se.

Please note that the method described above will not correct links in files belonging to the website, just the database will be fixed. You may have to edit files on the site as well, which can be done e.g. via the File Manager accessible via cPanel.

Create an administrator account

Sometimes, it may happen that WordPress erases the permissions for an administrator account. When this has happened, you’ll see an error message when you try to log in using that admin account, instead of reaching wp-admin.

Once you’ve navigated to the document root of the WordPress installation (see above), you can create a new admin user via WP-CLI using this command:

wp user create USERNAME EMAILADDRESS --role=administrator

Swap out USERNAME in the command above with the username you want the new admin user to have, and swap EMAILADDRESS to the email address for the new user.

Use WP-CLI in cronjobs

If you want to use WP-CLI in cronjobs on your web hosting account, you need to call the command this way

/usr/local/bin/php /usr/local/bin/wp X

instead of just typing wp in the beginning. This is necessary because cron runs in another environment than the one you end up in when you connect to the account via SSH. Replace X in the command above with the rest of the command that should follow wp if you were to run the WP-CLI command via SSH.

If you want to use a specific PHP version in the cron job, enter /opt/alt/php73/usr/bin/php instead of /usr/local/bin/php.

You can choose what PHP version to run by switching out 73 in the command above for another of the available PHP versions (e.g. 70 if you want to use PHP 7.0).

Was this article helpful?

Related Articles