Install WordPress through WP-CLI – Everyone is probably familiar with WordPress and its renowned 5-minute install routine. It’s simple and works without fuss. But when you have multiple sites to manage, repeating the same routine can waste plenty of time which you could have used elsewhere.
This is where WP-CLI, a powerful command line tool with which you can manage WordPress, can help. This tutorial covers how to install WP-CLI and how to perform some common, practical tasks using it.
- WP-CLI is available as a PHP Archive file (.phar). You can download it using either wget or curl commands:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- You need to make this .phar file executable and move it to /usr/local/bin so that it can be run directly:
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
- Check if it is installed properly:
You should see a similar output like that displayed above, which means you can proceed:
You can use the above three steps for upgrading WP-CLI as well.
Basics of WP-CLI
Before moving on, let’s learn some basics of how WP-CLI works. This will help you feel comfortable with the upcoming steps.
So far, we have seen WP-CLI accessed through the main command,
wp. You can follow the main command with nested subcommands. For example, we have a command to download WordPress, which is:
wp core download
Here wp is the main command while core and download are its nested subcommands. Nesting subcommands can extend one or two levels.
WP-CLI also comes with a detailed help section, which displays all the commands you might need. To access help:
The output should resemble:
: is a prompt that, with subcommands, can help you navigate through this help menu. Up and down arrow keys will let you scroll through the entire help command list. Typing q will exit the help menu. For additional details on how to further navigate through the complete help section, you can always type h at the above prompt.
You can use the enabled bash completion to demonstrate WP-CLI’s readily available command list. Simply type wp and press tab twice. You will see the list of available commands. Now, type wp core and press tab twice. You will see a list of commands that can be used with core. This double tabbing after a command can be repeated for any primary or subcommand.
Setting up Database
- Before you proceed, you need to setup a database. Log in to the MySQL server, replacing user with your MySQL username:
mysql -u user -p
- Create a database:
create database wordpress;
- Grant required privileges to the database for the mysql user to which WordPress will permit database access. Replace user and password with those of the permitted mysql user:
grant all on wordpress.* to 'user' identified by 'password';
- Type quit to exit the MySQL command line.
- Move to the Apache test directory:
- Next, download the WordPress files.
wp core download
- Create a wp-config.php file:
wp core config --dbname=wordpress --dbuser=user --dbpass=password --dbhost=localhost --dbprefix=wp_
dbhost and dbprefix are entirely optional and can be omitted unless you need to change their default values.
- Run the installation:
wp core install --url="http://example.com" --title="Blog Title" --admin_user="adminuser" --admin_password="password" --admin_email="firstname.lastname@example.org"
Your WordPress blog is now ready for use.
Installing Themes with WP-CLI
Importing and installing themes is much quicker through WP-CLI than going into WordPress admin, searching and then activating it.
It connects your server directly to the WordPress theme repository and imports the theme in a matter of a few seconds. For example, to install a theme like TwentyTen, we will use the following command:
wp theme install twentyten
To activate the themes on your WordPress website, you will need to use the following command:
wp theme activate twentyten
Installing Plugins with WP-CLI
Just like the themes, plugins can also be installed right from the official repository. Installation is seamless and takes almost no time.
wp plugin install woocommerce
The above command will install the WooCommerce plugin on your website. To activate it, we will use the command:
wp plugin activate woocommerce
Similarly, to deactivate any plugin, the command becomes:
wp plugin deactivate woocommerce
Updating WordPress Core, Themes and Plugins with WP-CLI
Repetitive and often time consuming task of updating the WordPress core, themes and plugins can be easily carried out through the command line.
To update the WordPress core to the latest stable release of WordPress, the command is:
wp core update
If you want to update to a specific version, for example you have WordPress 3.7 and want to move to WordPress 4.0 instead of WordPress 4.3, use the command:
wp core update --version=4.0
Or, if for any reason you want to revert back your WordPress site to the previous version, the command is:
wp core update --version=3.9 --force
To update plugins, you can either define one plugin to update or better still.
wp plugin update woocommerce
Update all the plugins in one go.
wp plugin update --all
A similar method applies to the themes. You can update WordPress themes through WP-CLI with following command
wp theme update twentyten
Update all the themes in one go.
wp theme update --all
WP-CLI is indeed a powerful tool that can be used to manage your WordPress sites through the command line. There are also many more commands to manage your database, take backups, manage posts, comments and WordPress multisite It definitely speeds up your tasks, and makes it really easy to manage the sites. Luckily, there are hosting providers that provide WP-CLI by default on their WordPress installations (you can find a maintained list here).