Skip to content

Instantly share code, notes, and snippets.

@Oceas
Last active September 16, 2019 16:20
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/0d6e7341a367428af3da6336b6f999fa to your computer and use it in GitHub Desktop.
Save Oceas/0d6e7341a367428af3da6336b6f999fa to your computer and use it in GitHub Desktop.
WP-CLI 101 Messages
/**
* Returns multiple messages to demonstrate different command return types.
*
* @since 1.0.0
* @author Scott Anderson
*/
public function display_messages( $args, $assoc_args ) {
// No prepends.
WP_CLI::line( 'Standard line return.' ); // No prefix on line return.
WP_CLI::log( 'Standard line returned that wont be silenced.' ); // No prefix on line but ignores --quiet
// Color make sure to use %n at the end of a style or else the style will apply to the next output.
WP_CLI::line(WP_CLI::colorize( '%BBlue text%n' )); // Returns text in blue color. Ignores --quiet.
WP_CLI::line(WP_CLI::colorize( '%MMagenta text%n' )); // Returns text in magenta. Ignores --quiet.
WP_CLI::line(WP_CLI::colorize( '%UUnderline text%n' )); // Returns text underlined. Ignores --quiet.
// Only prepends.
WP_CLI::success( 'Post updated!' ); // Prepends Success to message
WP_CLI::warning( 'No match was found.' ); // Prepends Warning to message.
// Special conditions.
WP_CLI::debug( 'Breakpoint comment.' ); // Displays only when --debug flag is used.
WP_CLI::error_multi_line( ['Error found!', 'Post not updated!','User not updated!'] ); // Displays multi-line error in red box. Doesn't exit script. Ignores --quiet but looses formating.
//Returns error message if --error custom flag is added.
if( isset($assoc_args['error']) && $assoc_args['error']) {
WP_CLI::error( 'Error found!' ); // Prepends message with Error and exits script.
}
WP_CLI::halt( 200 ); // Halts script execution with a specific return code. Could for calling your subcommand from another.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment