WP-CLI: How to use wp-cli to manage Wordpress from cli
WP-CLI is a terminal interface through which you can work with WordPress directly via the terminal and SSH connection. You can, for example, download installation files, install plugin, and perform advanced edits over multiple sites at once.
Using WP-CLI you can manage your whole Wordpress website from shell console, with super-admin privileges. It saves your time and allows you to automatise some processes or help managing a bigger Wordpress websites and installations.
WP-CLI support is available on our VIP hosting. All you need is active SSH access.
WP-CLI Usage Examples
Install files download (Wordpress package download)
$ cd /web $ wp core download --path=dev/wp-cli Creating directory '/home/sourcecode.sk/sub/dev/wp-cli/'. Downloading WordPress 4.9.8 (en_US)... Using cached file '/home/.wp-cli/cache/core/wordpress-4.9.8-en_US.tar.gz'... Success: WordPress downloaded.
With the "wp core download" command, you download the installation files to the folder you are currently in. In the example above, we've set --path=dev/wp-cli to set the files to the dev/wp-cli folder in main web folder.
Creating a new wp-config.php file
Before you start creating a wp-config file, create a new MySQL Database with a new database user first.
$ cd /web/dev/wp-cli $ wp config create --dbname=wpclidemo --dbuser=wpclidemuser --dbpass=password --dbhost=mysql5-1 Success: Generated 'wp-config.php' file.
Entering the "wp config create" command you create a new wp-config.php file with your database data settings. At this point, it is worth mentioning that the "wp db create" command will no longer work because of the nature of our web / DB servers - you can not create a new database using WP-CLI. However, you can work in the local environment.
Creation of MySQL database and database user is available only in Hosting Control Panel.
Installing Wordpress with database migration
When you have downloaded Wordpress files, created a configuration file (wp-config.php), you can run Wordpress installation process.
In the directory where you have downloaded Wordpress and created wp-config.php file, just run (do not forget to adjust CAPS variables by your needs):
$ wp core install --url="ENTER_URL" --title="ENTER_WEBSITE_TITLE" --admin_user="ENTER_ADMIN_USERNAME" --admin_email="ENTER_ADMIN_EMAIL"
When process is completed, do not forget to copy auto-generated admin password.
With the "wp plugin update --all" command, you will instantly update all installed WordPress plugins in that folder. In the example below, you can also see the form of the output. You surely noticed that no WordPress password was needed - if you are connected via SSH, with WP-CLI you have full control over everything.
The command above update all you plugins, but you can update them selectively:
$ wp plugin update PLUGIN_NAME
Where PLUGIN_NAME is name of the plugin you want to update.
To update all your installed themes just enter in the Wordpress directory:
$ wp theme update --all
You can also update themes selectively:
$ wp theme update THEME_NAME
Where THEME_NAME is name of the theme you want to update.
Updating Wordpress installation is also very easy. You can update whole Wordpress installation by command. In the Wordpress directory run:
$ wp core update
More useful WP-CLI commands
To list of all WP-CLI useful features please visit WP-CLI Guide (link bellow), or you can use WP-CLI Help by entering:
$ wp help
We consider these functions to be very useful:
- wp comment - Creates, updates, deletes, and moderates comments.
- wp core - Downloads, installs, updates, and manages a WordPress installation.
- wp cron - Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
- wp export - Exports WordPress content to a WXR file.
- wp import - Imports content from a given WXR file.
- wp media - Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
- wp option - Retrieves and sets site options, including plugin and WordPress settings.
- wp plugin - Manages plugins, including installs, activations, and updates
- wp post - Manages posts, content, and meta.
- wp role - Manages user roles, including creating new roles and resetting to defaults.
- wp site - Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
- wp super-admin - Lists, adds, or removes super admin users on a multisite installation.
- wp theme - Manages themes, including installs, activations, and updates.
- wp user - Manages users, along with their roles, capabilities, and meta.