WordCamp 2019 - Custom WP-CLI commands

could be really useful for Patrick @patrick
what is WP-CLI

  • command line interface
  • interact from command line/scripting
  • can have different outputs - like json or csv instead of pretty print
  • 44 commands out of the box
  • anything you can do in admin you can do in cli
  • benefits
    • chainable and composable scripting (if/then etc)
    • help surfaces phpdoc
    • multisite commands
    • remote control - use one install of wp-cli to take command of another
  • format helper/arg can be useful and when adding custom command you can change which output format to use

USEFUL FOR PLUGIN DEATHS @patrick

Basic command:

note the check for wpcli first or hook to cliinit, then:
add_command( cmdTitle, function or class reference, options => ( when, for instance )

As class:

then you would replace the function in the add_command call with the string 'Space::Base' so it registers the class and then the commands are registered from that

general best practices

  1. do one thing exceptionally well
    • you can run wp_cli commands within
  2. be consistent and parallel
    • start and stop v begin and end
    • avoid jargon in command names
    • look for brevity
  3. don't reinvent wheels
    • look for using other clis, maybe even wp_cli plugins instead of making your own
  4. build for humands and machines
    • so don't do interactivity prompts, or at least have an arg that can optionally be used to skip the prompt
  5. balance brevity and verbosity
  6. reuse wp_cli bundled commands
  7. write functional tests
    • ????

getflagvalue is best practice

class structure and using invoke
use PHPDoc format which will automatically fill in the help output

GitHub of intro to WP-CLI
https://github.com/0aveRyan/learn-wp-cli

wp-lando is a good simple one to see
https://github.com/0aveRyan/wp-lando/blob/master/inc/class-command.php

CLIMate helps make simple PHP UIs

Testing

behat is the testing set
scaffold-command in actual wp-cli has FEATURES tests writting in behat
http://behat.org/en/latest/

More from Summerlin
All posts