Cs_looper_custom formatting JSON data

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.

Hi @bobbybosler,

Thanks for reaching out.
Not sure what do you mean by the Dynamic Content to format the data. If you are trying to format the data in the Dynamic Content, unfortunately, there is no such option currently with the existing Dynamic Content tags. But you can create your own Dynamic Content tags, where you can format the data or parameters sent to the respecting functions called against it.
I would suggest you go through the following article on how to create your own Dynamic Content tags.

https://theme.co/forum/t/pro-5-0-5-ermmm-where-did-my-save-button-go/91790/10

https://theme.co/forum/t/bug-pro-5-1-rc3-dynamic-content-telephone-link/93707

If that is not the case, I would request you explain it a bit more to understand your need.

Thanks

@tristup,

I can’t access those articles—it says they are either private or do not exist.

Hi @bobbybosler,

You need to login into your themeco account to access those pages.

Hope that helps.

@marc_a,

I am.

Hi @bobbybosler,

These URLs are beta testing URLs, I would suggest you enable the option from your Themeco Account and check if you are able to access the threads.

Screenshot-1069-

Hope it helps.
Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.