Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code for using the Caldera Forms REST API JavaScript client. See: https://calderaforms.com/doc/caldera-forms-rest-api/
/**
* Get form config details
*
* api is instance of CFAPI
*/
var form = api.getForm();
/**
* Get first page of entries
*
* api is instance of CFAPI
*/
var entries = api.getEntries();
/**
* Get first page 7 of entries
*
* api is instance of CFAPI
*/
var entries = api.getEntries( 7 );
<?php
/**
* Load Caldera Forms API client
*
* Requires Caldera Forms 1.5 or later
*
* Only works for admins
*/
add_action( 'wp_enqueue_scripts', function(){
//change this to your form ID
$form_id = 'CF123456';
//load api client JavaScript
Caldera_Forms_Render_Assets::enqueue_script( 'api-client' );
//Print useful info to DOM
$config = new Caldera_Forms_API_JsConfig( Caldera_Forms_Forms::get_form( $form_id ) );
wp_localize_script( Caldera_Forms_Render_Assets::make_slug( 'api-client' ), 'MYOBJ', $config->toArray() );
});
/**
* Initialize Caldera Forms REST API client
*
* For admin users only
*/
jQuery(function() {
if( 'object' == typeof MYOBJ ){
//25 is the number of entries per page to get
var api = new CFAPI( MYOBJ.api.form, 25, MYOBJ.api.formId, MYOBJ.api.nonce, $ );
}
});
/**
* Initialize Caldera Forms REST API client
*
* For used of users that the token is valid for
*/
jQuery(function() {
if( 'object' == typeof MYOBJ ){
var tokens = {
//REST API Nonce
nonce: MYOBJ.api.nonce,
//Special token for API
token: MYOBJ.api.token
};
//25 is the number of entries per page to get
var api = new CFAPI( MYOBJ.api.form, 25, MYOBJ.api.formId, MYOBJ.api.nonce, $ );
}
});
<?php
/**
* Load Caldera Forms API client
*
* Requires Caldera Forms 1.5 or later
*
* With support for a lower user role
*/
add_action( 'wp_enqueue_scripts', function(){
//change this to your form ID
$form_id = 'CF123456';
//load api client JavaScript
Caldera_Forms_Render_Assets::enqueue_script( 'api-client' );
//Print useful info to DOM
$config = new Caldera_Forms_API_JsConfig( Caldera_Forms_Forms::get_form( $form_id ) );
$data = $config->toArray();
//Will allow for viewing by a user of editor role or higher
$data[ 'api' ][ 'token' ] = Caldera_Forms_API_Token::make_token( 'editor', $form_id );
wp_localize_script( Caldera_Forms_Render_Assets::make_slug( 'api-client' ), 'MYOBJ', $data );
});
@robwilde

This comment has been minimized.

Copy link

@robwilde robwilde commented Nov 12, 2017

Hey @Shelob9 spent some time over the weekend playing with the API setup for a plugin I am creating. May I suggest outlining the necessity to adjust the default priority for wp_enqueue_scripts to something like 11 or higher.
I should have caught it sooner but this action is running before the caldera_forms plugin runs and registers the cf-api-client script, so there is nothing to enqueue. This may save someone a few mins and maybe hours of "What am I missing"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.