Table of Contents
I was working on an update to the Varnish plugin I’ve adopted, and one of the requested features was for more debugging tool. I’d added in a status page, but this plugin is also used by web hosts, and sometimes asking a customer “Can you go to this bad and send me the results?” is a time sink.
So why not add in a command line tool?
WP-CLI? WP-CLI?
I love WP-CLI. It’s a command line interface (CLI) for WordPress (WP) that lets you do most anything via the command line. You can install and activate plugins, update themes, even write posts and add users. But if you’re tech savvy, it’s also a great tool to automate and manage the minutia of WordPress maintenance drudgery.
I’d already built out a basic varnish flush command (wp varnish purge
) so adding on to it isn’t terribly difficult. But what was difficult was making the output what I wanted.
Start With an Array Start With an Array
No matter what you need an array of the correct format for this to work. I was already storing everything in an array I save in a variable called $results
that looks like this:
Array ( [varnish] => Array ( [icon] => awesome [message] => Varnish is running properly and caching is happening. ) [remote_ip] => Array ( [icon] => awesome [message] => Your server IP setup looks good. ) [age] => Array ( [icon] => good [message] => Your site is returning proper "Age" headers. ) )
I was initially doing this so I could loop and output all results with an icon and message on the status page, but translating this to wp-cli was a matter of taking the array and repurposing it.
WP-CLI Tables WP-CLI Tables
In order to add in a table output to WP-CLI, you use the format_items
function:
WP_CLI\Utils\format_items( $format, $items, $headers );
The $format
value is taken from $assoc_args['format']
(I set mine to default to table if it’s not defined). The $items
are your array, and the $headers
are another array of what your headers are.
This is the tricky part. You have to make sure your array of headers matches your array of items, and fulfills the output you desire. In order to do this, start with figuring out what you need to output.
In my case, I wanted a name, a status (aka the icon), and the message. This means my array looks like this: $headers = array( 'name', 'status', 'message' )
Rebuild The Array Rebuild The Array
Once I sorted out what the format was like, based on the headers, I built the items array as follows:
// Generate array foreach ( $results as $type => $content ) { $items[] = array( 'name' => $type, 'status' => ucwords( $content['icon'] ), 'message' => $content['message'], ); }
Remember, my $results
were already an array. I’m just making it look right here.
Final Results Final Results
How does it look? Like this:
+-----------+---------+-------------------------------------------------------+ | name | status | message | +-----------+---------+-------------------------------------------------------+ | varnish | Awesome | Varnish is running properly and caching is happening. | | remote_ip | Awesome | Your server IP setup looks good. | | age | Good | Your site is returning proper "Age" headers. | +-----------+---------+-------------------------------------------------------+
And that is a nice tool for people to debug.