I started work on a new plugin this week1 and I quickly started running into those little things that add up to a lot of wasted time while developing. So far that’s three things:
- Flushing a cache of data I retrieved from an API. (In this case, I process that data before caching it, so changes require a cache flush.)
- Saving setting data for the plugin.
- Getting the current setting data from the plugin.
In the past, I’ve done all kinds of silly things for these. For #1, I’ve hacked together one-off shell scripts, but usually just threw a delete_transient
call in the plugin file and hit refresh. Fun times when you forget to remove that!
For #2, I’ve often hacked together a prototype of the eventual UI, and set up all the various save handlers (because that’s fun in WordPress!) just so that I could get data into the plugin. Oh, and then I change the data structure in mid-development and now I have to change the UI just to get that new data in.2 Fun! Or, when that approach grew tiresome, I’d take a cure from my #1 approach, and maybe hardcode an update_option
call, or maybe override the getter function to return hardcoded data.
And #3 just comes in handy as a sanity check to see if the current data is what’s expected. Good for debugging. I won’t detail the hodgepodge of ways I’ve done this, you’re getting the idea.
This time, I started with a simple command: wp sekritplugin flush-cache
. Then the curtain was drawn back, and I realized that three common tasks I’ve done in the past could be:
wp sekritplugin flush-cache wp sekritplugin set --format=json < dummy.json wp sekritplugin get
The great part about this is that in order to expose this functionality to wp-cli, I have to have it in my plugin, and I'm forced to make cleaner plugin API design decisions.
And here's a familiar scenario: I'm working on the backend of the plugin, while my teammate is working on the frontend UI. I realize there's deficiency in the data structure, and push a change. Previously, this wreaked havoc (been there). But now, I just pop a dummy.json
file in our repo that replicates dummy content for the new data structure. My teammate runs:
wp sekritplugin set --format=json < dummy.json
…and is back up and running, no fuss, no muss. They can also look at the changset for the dummy.json
file to get a quick sense of how the data structure changed.
Another great thing is that I can leave all of the data saving and validation tasks right until the end, when the data structure is finalized. No wasted time changing things in a rapidly changing plugin.
I'm sure I'll come up with more use cases as I move along, I'm only three days into this particular project.
So, tl;dr: adding wp-cli commands to your plugin is great for:
- Encouraging good API design
- Collaborating amidst a changing data structure
- Efficiency
- Better debugging
Commands are easy to write, so what's stopping you?
Great post!
One minor thing: in your examples, you should replace `wp cli sekritplugin` with just `wp sekritplugin`. `wp cli` is a different subcommand: http://wp-cli.org/commands/cli/
LikeLiked by 1 person
You’re totally right, I have no idea how that snuck in there! Post edited.
LikeLiked by 1 person