If you are not familiar with the WordPress Command Line (WP-CLI) you should really check it out. There are a lot of uses for it, and I think some of it’s power comes in the form of what it can do for you when you are developing a website locally. While there are a number of plugins that can also do the same thing, they don’t offer the flexibility the command line offers and you still have to install, manage, and then remove the plugins later.
Generating Content to Use
When you are working on a blog roll, list of user profiles, or any other feature that rely on have a lot of content to make sure it is styled correctly, the CLI is here to help. There are several commands that come in handy for creating on the fly content.
For example, say you need to generate 100 users:
wp user generate --count="100"
This will create one hundred subscribers. You can also assign them to a specific role instead by using the –role flag.
wp user generate --count="100" --role="administrator"
To show your new list of users all you need to do is enter:
wp user list
Another command that I have found particularly helpful is for generating posts. Just like with the user command, you can specify a lot of information, just looking at the full command list you can get an idea for how powerful it is.
wp post generate [--count=<number>] [--post_type=<type>] [--post_status=<status>] [--post_author=<login>] [--post_date=<yyyy-mm-dd>] [--post_content] [--max_depth=<number>]
There are quite a bit of parameters here, besides the obvious count, post date, and status, you can also set the post type (page if you need a page) and max_depth which is the number of child posts you need to make as well. You can generate 5 generic posts by simply providing a value for the count flag:
wp post generate --count="5"
In addition to automatically creating numerous posts, you can also create them using a file. This can be extremely useful if you have the content you need for the pages and want a quick way to populate them, or if you simply have boilerplate ipsum text you’d like to use.
wp post create [<file>] [--<field>=<value>] [--edit] [--porcelain]
As you can see there are only a few base commands for creating a post however, you actually have the ability to do far more complex interactions. You’ll notice the field flag, here you can enter associative arguments that correspond to their parameters in wp_insert_post(). For example you can use –post_title to set the title of the post.
For me the most interesting thing that you can do with post create is supply it with a file to fill in the content. I’ve only began using this to input the content but someone with far more skill could probably integrate this into a function to pull content from a source like Google Docs.
In this example I created a plain text file on my desktop and feed it to the function, creating a new post in the process.
wp post create /Users/lukepettway/desktop/ipsum.txt --post_title="A Post From A File"
This will create a post with the title A Post From A File and use the ipsum text in that file to fill up the content. Pretty flipping cool right? Listing out your posts is as easy as typing in:
wp post list
One of the things I constantly find myself having to do when working on sites is regenerating thumbnails. Whenever you add a new image size you typically want to do this, there are plugins that handle it as well as programmatic ways to do it as well. WP_CLI provides a quick way to do this:
wp media regenerate
You will be prompted to confirm the command and then it will execute (use the –yes flag to bypass the confirmation). A list of changed files will show you what has changed.
Aside from regenerating thumbnails you can also import files too, it works similar to how you use a file to populate a post:
wp media import /Users/lukepettway/desktop/ipsum.txt
Hey it worked! This isn’t really any faster than dragging a file to upload to WordPress but again, that is where the scripting power of the command line can come in really handy, you could easily feed in boilerplate files without having to log into the admin.
Another super handy dandy command you might want to use are the plugin commands. You have the ability to update, delete, install, activate, and deactivate.
Deleting a plugin is as simple as typing in the name
wp plugin uninstall hello-dolly
Sorry Matt! Now that we feel bad about uninstalling it, all we have to do to make up for this tragedy is run the install command.
wp plugin install hello-dolly
Things are now how they were before. But what if we are using a plugin that isn’t in the WordPress plugin repo? No worries because we can specify a url or file from a path:
wp plugin install ../path/to/zipfile/awesome-plugin.zip
One of the biggest annoyances of working with any database is making a backup. It’s not hard, but it’s a lot of clicks to do something so simple yet so important. With the command line, you can create backups in a snap:
wp db export
This will create a database dump at the root of your site. For anyone working locally, you might need to add the path of your mysql bin in terminal (on AMPPS I did this export PATH=$PATH:/Applications/AMPPS/mysql/bin). One of the most powerful features, while not part of the db command, is search-replace. Just by the name you can probably guess what it does. It is a rather jam-packed command:
wp search-replace <old> <new> [<table>...] [--network] [--skip-columns=<columns>] [--dry-run] [--precise] [--recurse-objects] [--all-tables-with-prefix] [--all-tables] [--verbose] [--regex]
The biggest use you are going to have with this is when you need to migrate a database. Most of us are probably used to doing a SQL query in phpMyAdmin to update our sites which isn’t really so good of a practice. There are times when data is serialized or in weird tables, and sometimes those queries don’t always get everything. Search-Replace can manage to get all of that data safely and update it. It is pretty straightforward too:
wp search-replace 'http://mylocalsite.dev' 'http://mylivesite.com' --skip-columns=guid
By default, this is only going to run on core tables, however there are two flags, –all-tables-with-prefix and –all-tables, which specify whether to run on all tables in the database with or without a prefix.
The commands that I have covered are the ones that come stock with WP_CLI, there is actually a whole community dedicated to writing all sorts of awesome things, and you can even extend it yourself. Some of the notable additions include super-cache for managing W3 Total Cache, acf for working with Advanced Custom Fields, and any-ipsum a content ipsum generator.
There are a lot of uses for the WP-CLI, especially when it comes to working on a site locally. While some of these commands may offer more utility than others, a lot of the power lies within scripting these beyond their default usage. Sure some local dev setups especially those using VVV with VV to auto-install can import your plugins during the build process, for the rest of us not using those environments, we can right simple scripts to pull in frequently used plugins directly from our computers or on a file server.