Writing WP-CLI Commands That Work
by Steve Grunwell
Benefits
- Easier to perform bulk ops
- Better vis into WP internals
- Cron, cache, transients, etc
- Not subject to timeouts like browser
- Better filesystem access
- Ability to script (automation)
Good places to use it
- Deploying updates
- Troubleshooting issues with cron, caching, transients
- Scheduled maintenance routines
- Data migrations and transforms
- YOLO scripts – scripts that can be run once and should never be available in the admin
- Serialized search and replace
What makes a good command?
- Don’t assume anything
- The command should be as portable as possible
- Don’t assume themes/plugins are present
- Composability
- Idea: command can be chained (e.g. Gulp)
- Readability trumps # of keystrokes
- Make commands/flags overtly clear
- Stay focused
- WP-CLI commands should leverage existing WP functionality
Basic Structure
- Basic OOP – class extends base class with private & public methods
- Docblock
- Description
- Options – args accepted by command
- Examples
- Tags
- @synopsis – used to validate args
- @subcommand – set canonical name for command (defaults to method name)
- @alias – additional handle
I/O
Input
- Args and assoc_args
- Confirm
- Prompt
- Menus
Output
- log
- warn
- error
- success
- tables
FORMAT_ITEMS()
- progress indicator
More good ideas
--dry-run
– good for trialing destructive runs--verbose
--quiet
- great addition to plugins
if ( defined( 'WP_CLI' ) && WP_CLI ) { require_once dirname( __DIR__ ) . 'my-wp-cli-plugin.php'; }
- use
wp_parse_args
Performance tips
- Try to cache whenever possible, but be careful of what persists
SAVEQUERIES
STOP_THE_INSANITY
Considerations
- Bash completion not included by default, available via .org
- Beware in multisite which site you’re working on
Useful globals
--user
– set user--debug
– show all PHP errors--prompt
– prompt user to enter values
Gotchas
die()
andexit()
kill the entire process – usereturn
instead- you can call other CLI commands from within your command
WP_CLI::sub_command( ... )
Build Your Dreams With The WordPress REST API & Javascript
by K.Adam White
Recent reqs fulfilled prior to merging in WP 4.7
- Meta handling
- Password-protected posts
- Site settings endpoint
- Improved consistency/examples
Conditional merge with success metrics monitoring:
- Plugin utilization
- Distributed theme utilization
- Custom development implementations
- Core dev and feature projects
Plusses
- Common data
- Common models
Stronger Than Fear: Mental Health In The Developer Community
by Ed Finkler
Empathetic Communication
by Sharon Steed
Creating The Community You Crave
Unix Philosophies for WordPress Development
by JJJ
WordPress is the Windows of the web.
– Justin Rohrer
The lesson to be learned from this is that it is often undesirable to go for the right thing first. It is better to get half of the right thing available so that it spreads like a virus. Once people are hooked on it, take the time to improve it to 90% of the right thing.
– Richard P. Gabriel
Resources & Reading
- http://github.com/wp-cli/php-cli-tools
- REST API
- https://osmihelp.org/ – mental health in the tech world
- https://wordpress.org/plugins/wp-lcache/ – alternative object cache, APCu (not Redis)
Hi.
I’m interested about WordPress plugins and as I see you didn’t updated your plugin – https://wordpress.org/plugins/better-font-awesome/ over 10 months.
I have a question and I will be very thankful if you contact with me.
I think you already have my email 🙂
Thanks.