How to Speed Up WordPress Development and Maintenance With WP-CLI

As a WordPress developer, you’ve probably installed the WordPress CMS, updated it, and activated themes and plugins hundreds of times. And although these routine development and maintenance tasks are fairly easy to do with WordPress’ graphical user interface, doing them over and over again isn’t very efficient.

The good news is that you can easily and effectively speed up WordPress development and maintenance with the WordPress Command Line Interface (WP-CLI). With this in mind, in this post, we’ll explore the different ways you can use WP-CLI and offer some helpful WP-CLI commands to help you get started with a step in the right direction.

Let’s put everything into context before we begin.

An Introduction to WP-CLI

If you’ve been in web development for some time then you’re probably familiar with the command line interface. WP-CLI is WordPress’ command line interface. And for those of you who haven’t dabbled much in programming, WP-CLI is a tool that allows you to manage WordPress installations without using a web browser.

With WP-CLI, you’re essentially trading in the intuitive WordPress GUI for increased productivity. It lets you do everything from installing the WordPress CMS to a brand new website to performing site-wide operations on an existing WordPress website. And the best part is that the set of steps you need to follow in order to complete those tasks will, in most cases, be reduced to a single line of code.

Now that you have a fair understanding of what WP-CLI is and how it can help you speed up development on your next project, let’s take a look at how you can get started with this powerful tool.

Installing WP-CLI on Your Hosting Environment

The very first thing you’ll need to do to get started with WP-CLI is make sure that your hosting environment meets the minimum requirements:

  • UNIX-like environment.
  • PHP 5.3.29 (or later).
  • WordPress 3.7 (or later).
  • Secure Shell (SSH) enabled on your web server.

Once you’ve crossed those essentials off your list, go ahead and download the wp-cli.phar file using the following command:

//GITHUB LINK: https://gist.github.com/rafaysansari/84324bd7f04b33553ce9a10883906b48

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

That’s it! WP-CLI should now be installed to your hosting environment. If you’d like to make sure it’s working properly, simply run the following command:

//GITHUB LINK: https://gist.github.com/rafaysansari/4aa51d23be81f615c1585acca009cab7

$ php wp-cli.phar -info

If all goes well, you should see something like this on the command line specifying which version of WP-CLI your hosting environment is running:

//GITHUB LINK: https://gist.github.com/rafaysansari/59d84de336df501eb4615834aa074fdb

PHP binary: /usr/bin/php5
PHP version: 5.5.9-1ubuntu4.14
php.ini used: /etc/php5/cli/php.ini
WP-CLI root dir: /home/wp-cli/.wp-cli
WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
WP-CLI global config: /home/wp-cli/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 1.3.0

However, if you find that WP-CLI didn’t install properly on your system, we recommend that you check out some alternative installation methods for more information on how to set it up.

Finally, we’ll create an executable file for WP-CLI and move it to its own directory so that we can run it from anywhere:

//GITHUB LINK: https://gist.github.com/rafaysansari/f622ee4726a3a6624399e207a39ebf2b

$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp

For the sake of simplicity, we’ve name the directory wp. Now, whenever you need to use WP-CLI all you have to do is run the wp command from the command line.

WordPress Development and Site Maintenance With WP-CLI

Now that you have WP-CLI installed and ready to go, let’s walk through some of the most useful things you can do with it to speed up routine WordPress development and maintenance tasks.

Installing WordPress

Make your way into the directory where you want to install the WordPress CMS and run the following line of code:

//GITHUB LINK: https://gist.github.com/rafaysansari/dcc07ad1efaf3114a83aaa044368a20b

wp core download

And you’ll need to create a wp-config.php file to go with your installation. Here’s how you can do that:

//GITHUB LINK: https://gist.github.com/rafaysansari/bb3adbd51c000bd1e29867fa50fde0c4

wp core config --dbname=databasename --dbuser=databaseuser --dbpass=databasepassword --dbhost=localhost --dbprefix=prfx_

(We’ve used placeholder text for the database name and database user’s credentials. Be sure to replace them with your database’s information before running the code.)

Finally, you can begin the actual installation by running the core install command given below. Remember to replace the placeholder parameters with your site’s information before executing the code.

//GITHUB LINK: https://gist.github.com/rafaysansari/749244bb1281a4be96fb10178ffaa19b

wp core install –url=yoursite.com –title="Your WordPress Site's Title" –admin_user=admin_username –admin_password=admin_password –admin_email=admin@yoursite.com

Updating WordPress

Sooner or later a new version of WordPress will roll out and you’ll need to update your installation to the latest version. If you’re not sure which version of WordPress your site is currently running, simply run the following command:

//GITHUB LINK: https://gist.github.com/rafaysansari/bc63a66529de5a1a3c89bbd0b06cd445

wp core version

If you find that your site does indeed need to be updated then it’s best to take a full backup of its database before proceeding. Here’s how you can do it with WP-CLI:

//GITHUB LINK: https://gist.github.com/rafaysansari/adab80933f2f966b852968fcbe9dbb97

wp db export my-db-backup.sql

Running this command will create a complete backup of your site’s database and save it to your root directory in a file called my-db-backup.sql.

Finally, you can update your site’s core files and its database by running the following lines of code:

//GITHUB LINK: https://gist.github.com/rafaysansari/418f43ad3dced17c47ce635e61d5251c

wp core update
wp core update –db

For those of you who manage multiple sites or multisite networks, run the following script to update all of your sites in one go:

//GITHUB LINK: https://gist.github.com/rafaysansari/99db0f16a7df0d8dcf21aef979f9764b
#!/bin/bash
declare -a sites_to_update=('/var/www/wordpress_site_1' '/var/www/wordpress_site_2' '/var/www/wordpress_site_n')
for site in "${sites_to_update[@]}";
do
wp --path=$site core update
done

(Remember to replace the placeholder text with the names of the root directories of your WordPress websites.)

Managing Themes and Plugins

One of the best things about WP-CLI is that it connects your web server to the official WordPress Theme and Plugin directories. What this means is that you can manage your theme and plugin installations using only the command line.

WordPress Theme Commands:

  • To install a theme: wp theme install theme_name
  • To activate an installed theme: wp theme activate theme_name
  • To update an installed theme: wp theme update theme_name
  • To update all installed themes: wp theme update –all

WordPress Plugin Commands:

  • To install a plugin: wp plugin install plugin_name
  • To activate an installed plugin: wp plugin activate plugin_name
  • To update an installed plugin: wp plugin update plugin_name
  • To update all installed plugins: wp plugin update –all

(Remember to replace the placeholder text with the name of the theme or plugin that you’d like to interact with.)

Creating Custom Post Types

WP-CLI takes the heavy lifting out of creating custom post types in WordPress and reduces it to a simple, single line of code. Instead of downloading a plugin to help you get the job done why not try out the following line of code:

//GITHUB LINK: https://gist.github.com/rafaysansari/c1e0bcc2d23a205a95b8a6af89a1762e

$ wp scaffold post-type cpt_slug --label=CPT_Label --theme=theme_name

All you have to do is replace the placeholder text with your custom post type’s slug, label, and theme name and you’re good to go.

Creating Child Themes

If you’ve ever created a child theme then chances are you had to mess around with a bunch of WordPress’ core files after accessing them via cPanel or an FTP client. WP-CLI lets you create a child theme with a single line of code:

//GITHUB LINK: https://gist.github.com/rafaysansari/fe3770d94b0261a8170e92aaba318f33

wp scaffold child-theme name-of-child-theme --parent_theme=name_of_parent_theme --theme_name='My Child Theme' --author='Your Name' --author_uri=http://www.yoursite.com --theme_uri=http://www.themesite.com --activate

(As always, remember to replace the placeholder text with your child theme and parent theme’s information.)

Conclusion

With WP-CLI, you can accomplish more by doing less. If you’d like to increase your productivity by speeding up routine WordPress development and maintenance operations then WP-CLI is definitely worth trying out.

We showed you how to install the tool to your hosting environment and walked you through some scenarios where WP-CLI beats the WordPress GUI in terms of efficiency. Hopefully, you’re in a good position to take things further yourself.

A professional writer, digital, and brand designer, Rafay's work is published across a number of high-authority sites and magazines. He has provided services to numerous brands across the globe and is the go-to solution provider to many reputable private and government organizations. He is also the co-founder of BloggInc. When he isn't overloaded with work, you can find him tending the farm with his wife, furniture hunting, and being awesome at in-door badminton. More articles by Rafay Ansari
Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress