WP_CLI::print_value PHP Method

print_value() public static method

Display a value, in various formats
public static print_value ( mixed $value, array $assoc_args = [] )
$value mixed Value to display.
$assoc_args array Arguments passed to the command, determining format.
    public static function print_value($value, $assoc_args = array())
    {
        if (\WP_CLI\Utils\get_flag_value($assoc_args, 'format') === 'json') {
            $value = json_encode($value);
        } elseif (\WP_CLI\Utils\get_flag_value($assoc_args, 'format') === 'yaml') {
            $value = Spyc::YAMLDump($value, 2, 0);
        } elseif (is_array($value) || is_object($value)) {
            $value = var_export($value);
        }
        echo $value . "\n";
    }

Usage Example

 /**
  * Verifies the API keys entered will work for writing and deleting from S3.
  *
  * @subcommand verify
  */
 public function verify_api_keys()
 {
     S3_Uploads::get_instance();
     // Boot
     $upload_dir = wp_upload_dir();
     // The upload file location on the local filesystem
     $s3_path = $upload_dir['basedir'] . '/' . mt_rand() . '.jpg';
     // Attempt to copy the file to S3
     WP_CLI::print_value('Attempting to upload file ' . $s3_path);
     // Copy canola from the test dir, upto S3
     $copy = copy(dirname(dirname(__FILE__)) . '/tests/data/canola.jpg', $s3_path);
     // Check that copy worked
     if (!$copy) {
         WP_CLI::error('Failed to copy / write to S3 - check your policy?');
         return;
     }
     WP_CLI::print_value('File uploaded to S3 successfully');
     // Delete off S3
     WP_CLI::print_value('Attempting to delete file ' . $s3_path);
     $delete = unlink($s3_path);
     // Check that delete worked
     if (!$delete) {
         WP_CLI::error('Failed to delete ' . $s3_path);
         return;
     }
     WP_CLI::print_value('File deleted from S3 successfully');
     WP_CLI::success('Looks like your configuration is correct.');
 }
All Usage Examples Of WP_CLI::print_value