JSON PROVIDER - How to inject in provider looper?

Hi, is it possibile to inject json array in json provider editing and not only wiriting hardcoded? Thank you

I was wondering the same thing.

I am also trying to find or create a script for Google Sheets that would export properly formatted JSON. There are some available, but the output seems to be different: http://blog.pamelafox.org/2013/06/exporting-google-spreadsheet-as-json.html. It would be great to have a solution to gerate proper code.

The ultimate option would be to be able to use WP All Import plugin’s option to keep a Google sheet synchronized with the array. :slight_smile:

1 Like

Or to be able to inject JSON from any API we are using :slight_smile:

1 Like

The JSON Provider is just static, but it’s entirely possible with custom development to create new Looper Provider types. Here’s the complete code for the JSON type:

class Cornerstone_Looper_Provider_Json extends Cornerstone_Looper_Provider_Array {

  public function get_array_items( $element ) {
    return json_decode( $element['looper_provider_json'], true );
  }

}

So you could make your own replacement of the JSON provider by doing something like:

class My_Custom_Data_Looper_Provider extends Cornerstone_Looper_Provider_Array {

  public function get_array_items( $element ) {
   $content = $element['looper_provider_json'];
    // do something with the code in the JSON control.
    return [];
  }

}

add_filter('cs_resolve_looper_provider', function($provider, $element) {

  if ($element['looper_provider_type'] === 'json') {
    $provider = new My_Custom_Data_Looper_Provider( $element );
  }

  return $provider;
}, 10, 2 );
 

We’ve not made any official API docs for this since we still want to do some more provider types and we may change how things work a bit internally, but hopefully this helps. I would caution anyone trying this out to make sure any remote API calls are cached, otherwise they will be called on every page load, and anytime the element re-renders in the preview (pretty much if anything inside the element changes).

2 Likes

Thank you very much @alexander.
I’m hourly surprised about you great work.
Thanks and thanks again

You’re welcome! And thank you for the kind words.

Sorry @alexander , I have tried to follow your way , but I encountered this issue:
PHP Fatal error: Uncaught Error: Class ‘Cornerstone_Looper_Provider_Array’ not found
Please,
what class we have to import in our functions.php?
Thank you very much

Hi @Papini,

It sounds like your code might be running too early, before Cornerstone has a chance to load all the classes. You can try this:

add_action( 'plugins_loaded', function() {
   // put your code here
   // or require another file like:
  require_once( '/path/to/looper-code.php');
} );