Skip to content

Instantly share code, notes, and snippets.

@hirozed
Last active January 17, 2021 05:29
Show Gist options
  • Save hirozed/0fe085f0fd43c57b5fd98fd1dda967c5 to your computer and use it in GitHub Desktop.
Save hirozed/0fe085f0fd43c57b5fd98fd1dda967c5 to your computer and use it in GitHub Desktop.
WP CLI Skeleton
<?php
/**
* CLI commands
* https://make.wordpress.org/cli/handbook/guides/commands-cookbook/
*/
WP_CLI::add_command( '[command]', '[CLASS_NAME]' );
/**
* WP_CLI class
*
* @package [package]
*/
class [CLASS_NAME] extends WP_CLI_Command {
/**
* Description
*
* ## OPTIONS
*
* <arg>
* : Non-labeled argument.
*
* [--assoc-arg=<assoc-arg>]
* : Labeled argument.
* ---
* default: any
* options:
* - opt1
* - opt2
* ---
*
* ## EXAMPLES
* wp example arg --assoc-arg=value --url=URL
*
* @subcommand [function-with-dashes]
*/
public function [function-name]( $args, $assoc_args ) {
$default = array(
'[assoc-arg]' => '[value]',
);
$info = wp_parse_args( $assoc_args, $default ); // Merge incoming $assoc_args with $defaults.
$yay = 0; // Successful.
$boo = array( // Didn't work.
'count' => 0,
'slugs' => array(),
);
foreach ( $data as $key => $value ) {
if ( empty( $value ) ) {
WP_CLI::error( $key . ' variable must be filled out.' );
return;
}
}
}
}
/**
* Add to functions.php
*
* if ( defined( 'WP_CLI' ) && WP_CLI ) {
* require_once '[path-to-cli-script]';
* }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment