Hey there, I’m geeking out over the power of the custom looper function in Cornerstone. I whipped together a little looper to pull in JSON data from my Simplecast API and display the data points on my page, but I’m wondering if there’s a way in the Dynamic Content to format the data. For instance, one of my fields returns a date, which would be nice to be able to reformat to another date format. Another field is a large number, which would be nice to display with commas in the number for ease of reading.
You can see the demo page here: https://hcl.gfd.mybluehost.me/bobbybosler/sandbox/
And for those wondering, here is my code for the looper (custom hook is simplecast_api
):
My code in functions.php
// Custom HTTP Looper Request Function
add_filter( 'cs_looper_custom_simplecast_api', function( $result, $args ) {
$api_key = isset( $args['api_key']) ? $args['api_key'] : null;
$url = isset( $args['url']) ? $args['url'] : null;
$endpoint = isset( $args['endpoint']) ? $args['endpoint'] : null;
$path = !empty( $args['path']) ? $args['path'] : null;
$podcast = !empty( $args['podcast']) ? "podcast=" . $args['podcast'] : null;
$episode = !empty( $args['episode']) ? "&episode=" . $args['episode'] : null;
$interval = !empty( $args['interval']) ? "&interval=" . $args['interval'] : null;
$sort = !empty( $args['sort']) ? "&sort=" . $args['sort'] : null;
$start_date = !empty( $args['start_date']) ? "&start_date=" . $args['start_date'] : null;
$end_date = !empty( $args['end_date']) ? "&end_date=" . $args['end_date'] : null;
// This assembles the endpoint and the parameters to form a working url.
$assembled_url = $endpoint . "?" . $podcast . $interval;
if ( !$url ) {
$url = $assembled_url;
}
if ( $url ) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Bearer " . $api_key,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$request = curl_exec($curl);
curl_close($curl);
$response = json_decode( $request, true);
$result = !$path ? $response : $response[$path];
}
return $result;
}, 10, 2);
And this is what you enter into the “param” field in the looper provider (with some obvious modifications for your info):
{
"_comment" : "The api_key field is required to operate this tool.",
"api_key" : "{{yourAPIkey}}",
"_comment" : "If the url field is filled, the remaining parameters will not function.",
"url" : "",
"_comment" : "If you wish to specify individual paramenets, fill them in below. ",
"endpoint" : "https://api.simplecast.com/analytics/downloads",
"path" : "by_interval",
"_comment" : "You must choose either a podcast or an episode.",
"podcast" : "{{podcast-id}}",
"episode" : "",
"_comment" : "You may choose from the following filter parameters.",
"interval" : "month",
"sort" : "desc",
"start_date" : "",
"end_date" : ""
}
This is my first time delving into this and I’m pretty excited about the world of possibilities this opens.