Den här artikeln finns även på: Svenska
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.
To be able to use WP-CLI
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
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!
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
://test2.se, make sure that you search for
http://test2.se and replace it with
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
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.