What is WP-CLI?
WP-CLI is a set of command line tools, executed through a console. WP-CLI allows you to manage multiple WordPress installations in your hosting account.
You can backup databases easily and quickly, automatically update WP, plugins and themes as well as automatically install several WordPress instances without using a web browser. This saves precious time when multiple WP installations are managed.
Depending on the tool specifications, you need WordPress 3.7 version or a later one.
Since the tool is used through a console, you also have to activate SSH access for your hosting account.
Using WP-CLI
Commands might be directly executed in a certain WordPress directory as well as outside the WordPress installation. The command specifies the installation that shall be modified.
For example, WordPress is installed in the following directory:
/home/cpaneluser/public_html/wordpress
Note: cpaneluser is the username for the hosting account.
Some basic wp-cli commands
To check the WordPress theme version you can type the following command in the console:
wp-cli theme status –path=/home/cpaneluser/public_html/wordpress
If you have already accessed the directory through the console, you can also execute the command:
wp-cli theme status
If the WordPress version is older than 3.4, you can update the system by using:
wp-cli core download –force
After the abovementioned command is executed, you will also need to update the database. Before that, be sure to perform a database backup:
wp-cli db export mydbfile.sql
The database named mydbfile.sql will be exported to the website’s directory.
The command for updating the database is:
wp-cli core update-db
To install a plugin you can use:
wp-cli plugin install –activate bbpress
If WordPress has been installed in a language different from English and you want to install a plugin or a theme, WP-CLI will try to install them in the same language. If the installed plugin or theme is not translated, the console might display warning messages. Despite them, the installation should be successful.
wp-cli configuration file
Further wp-cli settings can be configured in the wp-cli.yml file.
This file can be placed into the WordPress directory so that to apply for the website. If a global setting is necessary for all WordPress installations, the configuration file might be placed in home directory.
Update WordPress with cron job
If there are multiple WordPress instances installed into your hosting account and you decide to update them manually, the action may take quite a long time especially if there are many plugins.
You can try doing it automatically by using a configuration of few wp-cli commands in a file.
For example, first you have to create a new file in the hosting account’s home directory: /home/cpaneluser/wpupdateall.sh. Then type the following commands:
wp-cli db export --path=/home/cpaneluser/public_html/wp1
wp-cli db export --path=/home/cpaneluser/public_html/wp2
wp-cli core update --path=/home/cpaneluser/public_html/wp1
wp-cli core update --path=/home/cpaneluser/public_html/wp2
wp-cli core update-db --path=/home/cpaneluser/public_html/wp1
wp-cli core update-db --path=/home/cpaneluser/public_html/wp2
wp-cli plugin update --all --path=/home/cpaneluser/public_html/wp1
wp-cli plugin update --all --path=/home/cpaneluser/public_html/wp2
In these specific lines you need to add the correct path to the WordPress installation directories.
The database backup files will be saved to the hosting account’s home directory: /home/cpaneluser/
The wpupdateall.sh file permissions should be set to 0755. Then you should add the following cron job in cPanel:
/usr/bin/env PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/bin/wp-cli /home/cpaneluser/wpupdateall.sh
Note: cpaneluser is the username for the hosting account.
In a cron job you can start only one wp-cli command without creating additional file in the hosting account:
/usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /usr/local/bin/wp-cli db export mydbfile.sql --path=/home/cpaneluser/public_html/wp332
The results from the executed commands might be sent to you by email or saved in a log file.
Restore database backup
If after updating you notice problems with the website’s proper operation, you can restore the database backup or downgrade to the previous WordPress version.
Recover database from backup:
wp-cli db import wp1dbbackup.sql –path=/home/cpaneluser/public_html/wp
Downgrading to a previous WordPress version:
wp-cli core download –version=3.4 –force –path=/home/cpaneluser/public_html/wp1
(the command is on one line)
Do have in mind that an error message might occur when the website is accessed in case you decide to downgrade your WordPress and the version is considerably older than the current one.