WP_CLI::colorize PHP Method

colorize() public static method

Yes, you too can change the color of command line text. For instance, here's how WP_CLI::success() colorizes "Success: " WP_CLI::colorize( "%GSuccess:%n " ) Uses \cli\Colors::colorize() to transform color tokens to display settings. Choose from the following tokens (and note 'reset'): * %y => ['color' => 'yellow'], * %g => ['color' => 'green'], * %b => ['color' => 'blue'], * %r => ['color' => 'red'], * %p => ['color' => 'magenta'], * %m => ['color' => 'magenta'], * %c => ['color' => 'cyan'], * %w => ['color' => 'grey'], * %k => ['color' => 'black'], * %n => ['color' => 'reset'], * %Y => ['color' => 'yellow', 'style' => 'bright'], * %G => ['color' => 'green', 'style' => 'bright'], * %B => ['color' => 'blue', 'style' => 'bright'], * %R => ['color' => 'red', 'style' => 'bright'], * %P => ['color' => 'magenta', 'style' => 'bright'], * %M => ['color' => 'magenta', 'style' => 'bright'], * %C => ['color' => 'cyan', 'style' => 'bright'], * %W => ['color' => 'grey', 'style' => 'bright'], * %K => ['color' => 'black', 'style' => 'bright'], * %N => ['color' => 'reset', 'style' => 'bright'], * %3 => ['background' => 'yellow'], * %2 => ['background' => 'green'], * %4 => ['background' => 'blue'], * %1 => ['background' => 'red'], * %5 => ['background' => 'magenta'], * %6 => ['background' => 'cyan'], * %7 => ['background' => 'grey'], * %0 => ['background' => 'black'], * %F => ['style' => 'blink'], * %U => ['style' => 'underline'], * %8 => ['style' => 'inverse'], * %9 => ['style' => 'bright'], * %_ => ['style' => 'bright')
public static colorize ( string $string ) : string
$string string String to colorize for output, with color tokens.
return string Colorized string.
    public static function colorize($string)
    {
        return \cli\Colors::colorize($string, self::get_runner()->in_color());
    }

Usage Example

 /**
  * Migrate meta values to taxonomy terms. Delete meta after import
  *
  * ## OPTIONS
  *
  * <meta-key>
  * : Meta key to convert
  *
  * <taxonomy-slug>
  * : Taxonomy to move values into
  *
  * [--<field>=<value>]
  * : One or more args to pass to WP_Query.
  *
  * ## EXAMPLES
  *
  *     wp mtt migrate meta_key taxonomy_slug
  *     wp mtt migrate meta_key taxonomy_slug --posts_per_page=200 --paged=2
  *
  */
 function migrate($args, $assoc_args)
 {
     list($meta_key, $taxonomy) = $args;
     if (!($taxonomy_object = get_taxonomy($taxonomy))) {
         WP_CLI::error(sprintf("The taxonomy '%s' doesn't exist", $taxonomy));
     }
     $defaults = array('post_type' => $taxonomy_object->object_type, 'posts_per_page' => -1, 'post_status' => 'any');
     $query_args = array_merge($defaults, $assoc_args);
     $query = new WP_Query($query_args);
     // summary
     WP_CLI::log(sprintf("---\nPer page: %d \nPage: %d \nTotal pages: %d\n---", $query_args['posts_per_page'], isset($query_args['paged']) ? $query_args['paged'] : 1, $query->max_num_pages));
     while ($query->have_posts()) {
         $query->the_post();
         $id = get_the_id();
         // get meta
         $metas = get_post_meta($id, $meta_key);
         // create term
         if (!$metas) {
             WP_CLI::log(WP_CLI::colorize("%c[{$id}]%n No meta, skipped"));
         } else {
             if (!is_wp_error(wp_set_object_terms($id, $metas, $taxonomy, true))) {
                 WP_CLI::log(WP_CLI::colorize("%g[{$id}]%n Migrated: " . implode(', ', $metas)));
                 // clean meta
                 delete_post_meta($id, $meta_key);
             } else {
                 WP_CLI::log(WP_CLI::colorize("%r[{$id}]%n Error: Could not set terms for post"));
             }
         }
     }
 }
All Usage Examples Of WP_CLI::colorize