For many years “WordPress” was a dirty word amongst professional web developers. “You can’t use WordPress to build a serious web site.” “It’s for people who don’t know how to build a real web site.” “It’s fine if all you want is a blog.” These were the common refrains when the conversation of building a complex web application on WordPress came up, but at Mettle we are firm believers that every problem ought to be addressed with the right tool.
If you haven’t given WordPress a look in the past few years it’s definitely come a long way. Today, WordPress powers more than a quarter of all web sites and is able to easily handle applications well beyond the scope of “just blogs.” From full eCommerce systems to transaction-heavy online communities, a properly-configured WordPress application is capabile of quite a bit. I’d like to introduce you to a tool that can make setting up and maintaing a WordPress application even easier: WP-CLI
WP-CLI, as the name might imply, is a command line utility that you interact with from your computer’s terminal. If you’re not already comfortable with using your system’s terminal, this is definitely going to be a bit over your head—but I encourage you to take some time to get familiar with doing some of your work from the command line. You’ll gain access to a wealth of tools to make your workflow so much easier.
Installation: From 5 Minutes to 45 Seconds
If you’ve ever developed for WordPress, you know that it’s incredibly easy to get a basic framework up and running using their self-described “Famous 5-Minute Install”. And it is famous for good reason—by and large it just works: download the code, create your database, and go through the web-based configuration setup steps. But if you find yourself going through these steps on a regular basis and want to make this even faster—especially if your steps fall a little outside of the default setup—WP-CLI is here to save the day. Instead of those 5 minutes, you can install a full version of WordPress from the terminal:
$ wp core download $ wp core config --dbname=yourdb --dbuser=username --dbpass=supersecret $ wp db create $ wp core install --url=localhost:8888/test --title="WordPress Site" --admin_user=admin --admin_password=supersecret --admin_email="email@example.com"
At the end of running these four lines, you will have a complete, functional copy of the latest version of WordPress installed. Now I’ll admit, that’s a lot of typing. But if doing a WordPress installation is something you do with any regularity, and with any sort of pattern, this can save you a lot of time by keeping a standard WP-CLI configuration file at
~/.wp-cli/config.yml to set some good defaults:
core config: dbuser: username dbpass: supersecret core install: title: "WordPress site" admin_user: admin admin_password: supersecret admin_email: firstname.lastname@example.org
With this simple configuration you eliminate a lot of the parameters that will be the same with each site, cutting down on what you need to pass to the WP-CLI utility. Of course, there are many many more things you can add to this configuration file if you wish.
Don’t forget your themes and plugins
Getting the core WordPress installation in place is just the beginning. You’re definitely going to want to install some standard plugins:
$ wp plugin install wordpress-seo --activate $ wp plugin install wp-scss --activate $ wp plugin install block-bad-queries --activate $ wp plugin install facebook-comments-plugin --activate $ wp plugin install wordfence --activate
If you have a standard starter theme that you want installed and activated, that’s a one-liner as well:
wp theme install https://gitlab.com/mettle/clintonreno10year/archive/master.zip --activate
WordPress also makes the core upgrading process very, very easy. But if you find yourself managing hundreds (or even dozens) of WordPress sites it can become tedious to log into every site and click that “upgrade” button. That’s where WP-CLI comes in to save the day again:
$ wp core update
That’s it. One line in the terminal and your site is updated. Of course, the core WordPress files aren’t the only thing that requires occasional updates. You can manage the upkeep of your plugins and parent themes with WP-CLI as well.
$ wp plugin update --all $ wp theme update --all
WordPress security has come along way, especially if you’re following their best practices to avoid being a target of hacks, but every responsible WordPress developer needs to monitor their sites to make sure they haven’t become the victim of an intrusion. Well, WP-CLI has a few tricks to help there as well.
$ wp core verify-checksums
This simple command does a quick check of your core WordPress files against the original, signed code published by WordPress to ensure that the core files of your site have not been compromised.
WP-CLI is packed full of utilities that will save you time. Adding images to your WordPress media library is easy enough, but what do you do when you have a couple hundred images that you need to load up? I know what I do.
$ wp media import *.jpg
Or nest your commands, and reset the password for all administrator accounts
$ wp user update $(wp user list --field=ID --role=administrator) --user_pass=supersecret
If you’re moving your WordPress application to a new domain (or perhaps just migrating from a development server to production) you can use WP-CLI’s search and replace feature to save yourself some trouble there.
$ wp search-replace 'mettleup.dev' 'mettleup.com'
Let’s get crazy
All of this is pretty great. It saves us a good bit of time setting up a new WordPress site, installing some stock tools and making sure it stays updated and secure. But it doesn’t really help us build anything complex with WordPress. OK, let’s talk about the scaffold command. Scaffold straight-up builds your site for you, by generating files and PHP code that can be dropped right into your application. Creating the base files for a child theme, for example, is a one-liner.
$ wp scaffold child-theme mettleup --parent_theme=twentyfourteen --activate
If writing a theme from scratch is more your style you can get started with a boilerplate set of files for an Underscores theme (complete with Sass files—because we love Sass).
$ wp scaffold _s NewTheme --sassify --activate
What about generating custom post types and taxonomies? I’ll be honest, I almost always have to look up the documentation whenever I’m hacking around in the functions.php file. WP-CLI can actually generate this code for you.
$ wp scaffold post-type print >> $(wp theme path $(wp theme list --status=active --field=name) --dir)/custom-post-types.php $ wp scaffold taxonomy band --post_types=print --raw >> $(wp theme path $(wp theme list --status=active --field=name) --dir)/custom-post-types.php $ wp scaffold taxonomy color --post_types=print --raw >> $(wp theme path $(wp theme list --status=active --field=name) --dir)/custom-post-types.php
This will generate, all told, about a hundred lines of PHP code into a file called
custom-post-types.php in your active theme’s main folder. From there you can just add an include ‘custom-post-types.php’ directive in your
functions.php file and your theme will have a brand new post type of “Print” with the associated taxonomies of band and color. And all you had to do was write one line of PHP in your functions.php file.
OK, now let’s breathe
I know that was a lot of new information. The first time I learned about the power of WP-CLI my head was spinning with all the possibilties. It’s a super powerful tool and is in active development by the fine folks at WordPress, so you can expect it to get even more powerful as time goes by. As a matter of fact, just this past month they released the initial beta version of wp-rest-cli, which brings the power of WordPress’ new REST API into WP-CLI.
If you’re serious about WordPress development, working WP-CLI into your workflow is going to change the way you build, maintain, and grow your WordPress applications. Already addicted to WP-CLI like we are? Send us your best tips and we’ll share them here!