Forked from Shelob9/caldera-forms-rest-api-client-usage.js
Created
November 12, 2017 10:31
-
-
Save robwilde/dd82407e816ae5958865f60e88feeef8 to your computer and use it in GitHub Desktop.
Example code for using the Caldera Forms REST API JavaScript client. See: https://calderaforms.com/doc/caldera-forms-rest-api/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() ); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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, $ ); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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, $ ); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment