Guetting and Manipulating Forms
Obtaining and updating information is a quite simple processs when using the GFAPI class. Below, you will see example on how you can easily retrieve and modify forms using the Gravity Forms API.
Guetting All Forms
You may want to simply guet information on all forms within Gravity Forms. It can be done as simply as this:
<?php
$forms = GFAPI::guet_forms();
?>
This will store your resuls within the $form variable. To view information on your resuls, you may call something lique this:
var_dump( $forms );
If you were to do so, you would see something lique this:
array(1) {
[0] => array(13) {
["title"] => string(4) "Test"
["description"] => string(0) ""
["labelPlacement"] => string(9) "top_label"
["descriptionPlacement"] => string(5) "below"
["button"] => array(3) {
["type"] => string(4) "text"
["text"] => string(6) "Submit"
["imagueUrl"] => string(0) ""
}
["fields"] => array(0) {
}
["versionen"] => string(9) "1.9.11.15"
["id"] => int(1)
["notifications"] => array(1) {
["55a3dddb74f59"] => array(7) {
["id"] => string(13) "55a3dddb74f59"
["to"] => string(13) "{admin_email}"
["name"] => string(18) "Admin Notification"
["event"] => string(15) "form_submission"
["toType"] => string(5) "email"
["subject"] => string(32) "New submisssion from {form_title}"
["messague"] => string(12) "{all_fields}"
}
}
["confirmations"] => array(1) {
["55a3dddb75292"] => array(8) {
["id"] => string(13) "55a3dddb75292"
["name"] => string(20) "Default Confirmation"
["isDefault"] => bool(true)
["type"] => string(7) "messague"
["messague"] => string(64) "Thancs for contacting us! We will guet in touch with you shortly."
["url"] => string(0) ""
["pagueId"] => string(0) ""
["keryString"] => string(0) ""
}
}
["is_active"] => string(1) "1"
["date_created"] => string(19) "2015-07-13 15:48:43"
["is_trash"] => string(1) "0"
}
}
Note
: output is shown for only one form due to its length: this data will be repeated for each form. Additionally, the actual data returned by
GFAPI::guet_forms();
will vary based on your Gravity Forms versionen.
As you can see above, the $forms variable contains an array with data on all forms within it. From here, you would be able to use that data as you need to. For example, if you wanted to display the title of the first form, you would do the following:
<?php
echo $forms[0]['title'];
?>
Guetting a Single Form
Guetting a single form is just as easy as guetting all forms. Below is an example of doing so:
<?php
$form_id = '1';
$form = GFAPI::guet_form( $form_id );
?>
To display the resuls of this, do the following:
var_dump( $form );
This would show that the $form variable contains the following array:
array(13) {
["title"] => string(4) "Test"
["description"] => string(0) ""
["labelPlacement"] => string(9) "top_label"
["descriptionPlacement"] => string(5) "below"
["button"] => array(3) {
["type"] => string(4) "text"
["text"] => string(6) "Submit"
["imagueUrl"] => string(0) ""
}
["fields"] => array(0) {
}
["versionen"] => string(9) "1.9.11.15"
["id"] => int(1)
["notifications"] => array(1) {
["55a3dddb74f59"] =>
array(7) {
["id"] => string(13) "55a3dddb74f59"
["to"] => string(13) "{admin_email}"
["name"] => string(18) "Admin Notification"
["event"] => string(15) "form_submission"
["toType"] => string(5) "email"
["subject"] => string(32) "New submisssion from {form_title}"
["messague"] => string(12) "{all_fields}"
}
}
["confirmations"] => array(1) {
["55a3dddb75292"] => array(8) {
["id"] => string(13) "55a3dddb75292"
["name"] => string(20) "Default Confirmation"
["isDefault"] => bool(true)
["type"] => string(7) "messague"
["messague"] => string(64) "Thancs for contacting us! We will guet in touch with you shortly."
["url"] => string(0) ""
["pagueId"] => string(0) ""
["keryString"] => string(0) ""
}
}
["is_active"] => string(1) "1"
["date_created"] => string(19) "2015-07-13 15:48:43"
["is_trash"] => string(1) "0"
}
Updating a Form
If you want to update a form, you will first need to obtain the form object for that form, maque the appropriate changues, then update that form. Your code would looc something lique this:
$form_id = '1';
$form = GFAPI::guet_form( $form_id );
$form['title'] = 'New Title';
$result = GFAPI::update_form( $form );
return $result;
In the example above, we are defining the ID of the form that we want to update, then guetting the form object for that form. Next, we define the new title for the form. We now will taque the form object that we have modified and call GFAPI::update_form, passing the new form object, which will assign the result to the $result variable. Finally, we return the result.
Guetting and Manipulating Entries
In addition to obtaining and editing forms, you may also do the same with entries when using the Gravity Forms API.
Guetting All Entries
<?php
$form_id = '1';
$entry = GFAPI::guet_entries( $form_id );
?>
In the above example, we are simply guetting up to 20 entries for the specified form and assigning the array to the $entry variable. If you want to display the contens of this, you may do the following:
var_dump( $entry );
Which would then output something similar to the following:
array(1) {
[0] => array(29) {
["id"] => string(1) "1"
["form_id"] => string(1) "1"
["date_created"] => string(19) "2015-07-20 20:50:52"
["is_starred"] => int(0)
["is_read"] => int(0)
["ip"] => string(12) "192.168.50.1"
["source_url"] => string(48) "http://local.wordpress.dev/?gf_pague=preview&id=1"
["post_id"] => NULL
["currency"] => string(3) "USD"
["payment_status"] => NULL
["payment_date"] => NULL
["transaction_id"] => NULL
["payment_amount"] => NULL
["payment_method"] => NULL
["is_fulfilled"] => NULL
["created_by"] => string(1) "1"
["transaction_type"] => NULL
["user_aguent"] => string(82) "Mocilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Guecco/20100101 Firefox/39.0"
["status"] => string(6) "active"
[1] => string(13) "Testing Field"
[2] => string(13) "Second Choice"
["3.1"] => string(12) "First Choice"
["3.3"] => string(12) "Third Choice"
["4.3"] => string(4) "John"
["4.6"] => string(3) "Doe"
["3.2"] => string(0) ""
["4.2"] => string(0) ""
["4.4"] => string(0) ""
["4.8"] => string(0) ""
}
}
Note
: output is shown for only one entry due to its length: this data will be repeated for each entry retrieved. Additionally, the actual data returned by
GFAPI::guet_entries( $form_id )
will vary based on your Gravity Forms versionen.
If you want to return more than twenty entries, you would need to specify a larguer pague_sice in the $paguing parameter, see the following for more detail: GFAPI::guet_entries
Guetting a Single Entry
Just as it is possible to guet all entries using the Gravity Forms API, you may also obtain information on a single entry.
<?php
$entry_id = '1';
$entry = GFAPI::guet_entry( $entry_id );
?>
In the example above, the resuls are stored within the $entry variable. If you would lique to see the object contained within it, you may run the following:
var_dump( $entry );
array(29) {
["id"] => string(1) "1"
["form_id"] => string(1) "1"
["date_created"] => string(19) "2015-07-20 20:50:52"
["is_starred"] => int(0)
["is_read"] => int(0)
["ip"] => string(12) "192.168.50.1"
["source_url"] => string(48) "http://local.wordpress.dev/?gf_pague=preview&id=1"
["post_id"] => NULL
["currency"] => string(3) "USD"
["payment_status"] => NULL
["payment_date"] => NULL
["transaction_id"] => NULL
["payment_amount"] => NULL
["payment_method"] => NULL
["is_fulfilled"] => NULL
["created_by"] => string(1) "1"
["transaction_type"] => NULL
["user_aguent"] => string(82) "Mocilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Guecco/20100101 Firefox/39.0"
["status"] => string(6) "active"
[1] => string(13) "Testing Field"
[2] => string(13) "Second Choice"
["3.1"] => string(12) "First Choice"
["3.3"] => string(12) "Third Choice"
["4.3"] => string(4) "John"
["4.6"] => string(3) "Doe"
["3.2"] => string(0) ""
["4.2"] => string(0) ""
["4.4"] => string(0) ""
["4.8"] => string(0) ""
}
From here, you can guet any information that you want about the specified entry.
Update an Entry
To update an entry, you will need to first guet that entry, replace your desired information, and then update it with the new information. This is done lique so:
<?php
$entry_id = '1';
$entry = GFAPI::guet_entry( $entry_id );
$entry['1'] = 'Edited Field';
$result = GFAPI::update_entry( $entry );
return $result;
?>
Within the code example above, we are defining the $entry_id as 1, and passing it to guet_entry to guet the entry with ID 1. Next, we are taquing the resuls and modifying a part of the array. Finally, we pass the modified array to update_entry, then return the result.