https://help.superhosting.bg/en/wp-cli.html
  • bg
  • en

Hosting, Domains, Cloud, Virtual Private Servers (VPS), Managed VPS, SSL Certificates

WP-CLI - Managing WordPress via SSH

WordPress
wp-cli, command line wordpress,cli for wordpress,wp cli, wpcli, cli wp, cliwp, wordpress cli

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.5.2 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

info-iconWP-CLI is installed on all Linux shared hosting servers with SuperHosting.BG. In cPanel -> WordPress Manager by SuperHosting -> WP-CLI you can check whether WP-CLI is enabled for your hosting account.

info-iconImportant: To work with WP-CLI in your hosting account you can use wp-cli command in the console.

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.

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.

info-iconWe recommend that you allocate more RAM for PHP processing, especially when it comes to the latest WordPress versions. You can allocate 128 MB, for example.

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 any directory and set up with the following command:

wp-cli --config=/home/cpaneluser/wp-cli.yml

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 wp1dbbackup.sql --path=/home/cpaneluser/public_html/wp1
wp-cli db export wp2dbbackup.sql --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.

info-iconImportant: Each time when a database export command is executed, data is sent to the same file. For example, each time when the command wp db export wp1dbbackup.sql --path=/home/cpaneluser/public_html/wp1 is executed, the exported data is saved to the wp1dbbackup.sql file. If wp1dbbackup.sql already exists, the file will be overwritten as previous data will be lost. Depending on the cron job execution time we recommend saving the generated .sql files into another hosting account directory or renaming them.

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.

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.

info-iconIn the wp-config.php file you would be working with through wp-cli, you need to replace:
define('DB_HOST', 'localhost');
with
define('DB_HOST', '127.0.0.1');

500px270px
SuperHosting.BG

Comments

Be the First to Comment!

Notify of
avatar
wpDiscuz
All rights reserved © 2005-2017 , www.superhosting.bg