Skip to content

Instantly share code, notes, and snippets.

@Oceas
Last active September 19, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oceas/7260ca18a9186d960ea8322b1b8cebd9 to your computer and use it in GitHub Desktop.
Save Oceas/7260ca18a9186d960ea8322b1b8cebd9 to your computer and use it in GitHub Desktop.
WP-CLI 101 Function with Arguments
/**
* Returns all arguments passed into command to demonstrate how to access them.
*
* @param Array $args Arguments in array format.
* @param Array $assoc_args Key value arguments stored in associated array format.
* @since 1.0.0
* @author Scott Anderson
*/
public function display_arguments( $args, $assoc_args ) {
// Run command wp wds display_arguments John Doe 'Jane Doe' 32 --title='Moby Dick' --author='Herman Melville' --published=1851 --publish --no-archive
// Examples of Arguments.
WP_CLI::line( var_export($args[0]) ); // John
WP_CLI::line( var_export($args[1]) ); // Doe
WP_CLI::line( var_export($args[2]) ); // Jane Doe
WP_CLI::line( var_export($args[3]) ); // 32
// Example of Associated Arguments
WP_CLI::line( var_export($assoc_args['title']) ); // Moby Dick
WP_CLI::line( var_export($assoc_args['author']) ); // Herman Melville
WP_CLI::line( var_export($assoc_args['published']) ); // 1851
// Example of Associated Arguments as flag
WP_CLI::line( var_export($assoc_args['publish']) ); // True
WP_CLI::line( var_export($assoc_args['archive']) ); // False
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment