CSV Parser Shortcode Plugin


flickr photo shared by The Library of Congress with no copyright restriction (Flickr Commons)

The title just rolls off the tongue, yes?

But what it does is kind of interesting.

It’s not a drag/drop solution but it starts to show how we can better knit different services together. In this case, I run a Google Script to generate a spreadsheet of files (I can probably make that a straight up API connection in the near future). I publish that spreadsheet as CSV because Google sheets to JSON feels a good bit slower.1

Then the following shortcode (but w/in square brackets) generates the little unordered lists below. It’s taking that list of 464 files and just chopping out the ones where the filename’s first letters match A-1 and the parent folder is Tasks. Not brain surgery, right? But it is a pretty flexible solution to allow flexible access to any chunk of CSV (should work on any CSV on the web- assuming you adjust the elements to reflect the data) text you want to display portions of on the fly. People can do all their work in Google Drive and it seamlessly updates in WordPress. People without WordPress skills can update and change where these lists appear through a simple shortcode.

To be clear, this is an integration rather than an import. Nothing is being #reclaimed.

csvToList file=”https://docs.google.com/spreadsheets/d/1TbpX61BXYwSR-0Wb0-TcYoVY9ks7ElSLsheGNF2bLmM/pub?output=csv” sol=”A-1-” type=”Tasks”

and again with slightly different parameters

csvToList file=”https://docs.google.com/spreadsheets/d/1TbpX61BXYwSR-0Wb0-TcYoVY9ks7ElSLsheGNF2bLmM/pub?output=csv” sol=”A” type=”Instructional Materials”

If the plugin interests you, it’s over here.

The more interesting piece of the plugin is below.

$return_string = '<ul id="sols">';
    $substrLength = strlen($sol);
            $csv = array_map('str_getcsv', file($file));
            array_walk($csv, function(&$a) use ($csv) {
            $a = array_combine($csv[0], $a);
                                    });
            foreach($csv as $item) {
                $name = $item['Name']; //file name
                $short = substr($name, 0, $substrLength); //gets the part it'll search for
                $download = $item['Download']; //download file url
                $description = $item['Description']; //gets file description which no one uses in Google
                $folder = $item['Folder']; //gets the folder name
                $preview = $item['URL']; //preview URL
                if ($short == $sol && $folder == $type){
                $return_string .=  '<li><a href="' . $download . '">' . $name . '</a> - ' . $description . ' - <a href="' . $preview . '">preview</a></li>'; //mixes the stuff together for display
                }
            }
   $return_string .= '</ul>';
   return $return_string; //spits it back out



1 I have no data to back me up but I’m relatively sure that’s true.