Photo by Luca Bravo on Unsplash
This post was originally published on my personal blog
Hi, I'm Richard. I'm addicted to WordPress.
Oh, this isn't the support group? Fine. But nevertheless, let me tell you about a humble command-line tool called
wp (or WP CLI). It is by far my favorite way to manage WordPress installations and saves me from having to click through the Dashboard approximately 95% of the time.
wp installed isn't much of a hassle. If you already have Composer installed it's a breeze. You need only require the package and you're done.
I usually have
wp installed globally because I manage a lot of WordPress sites:
composer global require wp-cli/wp-cli
But of course you can also just require
wp for a project locally:
composer require wp-cli/wp-cli --dev
If you don't have Composer installed, there's also some alternative installation methods. Feel free to pick the one that best suits your workflow.
✨ Congratulations! Your life just became a whole lot more efficient!
Now to see just what you can do with
wp, let's install WordPress and configure it. All done from the cozy comfort of your shell. The very first thing we need to do is to download the latest version of WordPress.
$ wp core download Downloading WordPress x.y.z (en_US)... md5 hash verified: 83bec78836aabac08f769d50f1bffe5d Success: WordPress downloaded.
That wasn't hard at all, was it? Now let's setup our
wp-config.php with all the necessary information. For this you can run the following command
$ wp config create --dbname=wpdemo --dbuser=root --dbpass=root Success: Generated 'wp-config.php' file.
Of course you should replace the above database information with your own. The next step is to actually run the WordPress installer. That looks a bit like this
$ wp core install \ --url="https://wpdemo.test" \ --title="WordPress Demo Site" \ --admin_user="pretzelhands" \ --admin_password="awesome-p4ssw0rd" \ --admin_email="email@example.com" Success: WordPress installed successfully.
And you're done. You can now visit the URL of your WordPress site and take a look at your wonderful new installation!
That's a lot of parameters to remember. But never fear,
wp is here! Until you remember all these parameters you can also run
wp config create --prompt and
wp core install --prompt. These commands will interactively ask you for all required parameters. Nifty!
Now let's talk about some other awesome features you might want to have.
Check! Grab the slug of a plugin and you can run the following command:
$ wp plugin install advanced-custom-fields Installing Advanced Custom Fields (x.y.z) Downloading installation package from https://downloads.wordpress.org/plugin/advanced-custom-fields.x.y.z.zip... Unpacking the package... Installing the plugin... Plugin installed successfully. Success: Installed 1 of 1 plugins. $ wp plugin activate advanced-custom-fields
You can also pass in a
--activate flag to activate the plugin immediately. This looks like
$ wp plugin install advanced-custom-fields --activate
If you don't know the slug of a plugin you can find it by going to the plugin repository, searching for your plugin and then checking what it says after the last slash.
For example the URL for the Advanced Custom Fields plugin looks like this:
https://wordpress.org/plugins/advanced-custom-fields If you check everything after the last slash you can see the slug is
advanced-custom-fields. This is what you pass to
wp. It may take some time to remember what slug each plugin has, but you'll remember your favorites soon enough!
I hail from a German-speaking country. If I hand over a WordPress site with an English dashboard to a local client, they'll probably look at me weird. Thankfully, installing languages with
wp is a breeze.
$ wp core language install de_DE --activate Downloading translation from https://downloads.wordpress.org/translation/core/x.y.z/de_DE.zip... Unpacking the update... Installing the latest version... Translation updated successfully. Success: Language installed. Success: Language activated.
And you're done! Go grab some tea or coffee and enjoy your success. The
--activate flag here works the same way it does with plugins.
By now I'm sure you've figured out the pattern. But here's how you install a theme and activate it
$ wp theme install storefront --activate Installing Storefront (x.y.z) Downloading installation package from https://downloads.wordpress.org/theme/storefront.x.y.z.zip... Unpacking the package... Installing the theme... Theme installed successfully. Activating 'storefront'... Success: Switched to 'Storefront' theme. Success: Installed 1 of 1 themes.
And now you have Storefront for WooCommerce installed and running. Yes, that's really all there is to it.
The three examples given above showed you the most common use cases for
wp, but you can manage pretty much anything with it. Here's a short excerpt of what you can do without ever touching the WordPress dashboard
- Manage users
- Create, update, delete your posts
- Create, update, delete and moderate comments
- Do a search-replace in your database
- Export and import WordPress data
- Manage multi-site networks
- Scaffold code for custom post type, taxonomies, child themes, etc
- Run a custom PHP shell for your WordPress install
- Run a local development server for your Wordpress install
- ... (The list does go on for a while longer)
And if you install WooCommerce for example you get another set of commands related just to managing your online store.
So if you're working with WordPress, especially if you're working with more than 2-3 sites I heartily recommend you go out, install
wp and get more free time out of your day.
If you want to read more, you can check out the documentation, it is an excellent reference. Otherwise, a lot can be found out by running
wp help and browsing the man page.