Examples

Examples

Getting and Manipulating FormsGetting All FormsGetting a Single FormUpdating a FormGetting and Manipulating EntriesGetting All EntriesGetting a Single EntryUpdate an Entry

Getting and Manipulating Forms
Obtaining and updating information is a quite simple process when using the GFAPI class. Below, you will see example on how you can easily retrieve and modify forms using the Gravity Forms API.
Getting All Forms
You may want to simply get information on all forms within Gravity Forms. It can be done as simply as this:
123
This will store your results within the $form variable. To view information on your results, you may call something like this:
1var_dump( $forms );
If you were to do so, you will see something like this:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677array(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"      ["imageUrl"]=>      string(0) ""    }    ["fields"]=>    array(0) {    }    ["version"]=>    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 submission from {form_title}"        ["message"]=>        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) "message"        ["message"]=>        string(64) "Thanks for contacting us! We will get in touch with you shortly."        ["url"]=>        string(0) ""        ["pageId"]=>        string(0) ""        ["queryString"]=>        string(0) ""      }    }    ["is_active"]=>    string(1) "1"    ["date_created"]=>    string(19) "2015-07-13 15:48:43"    ["is_trash"]=>    string(1) "0"  }}
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:
123
Getting a Single Form
Getting a single form is just as easy as getting all forms. Below is an example on doing so:
1234
To display the results of this, simply do the following:
1var_dump( $form );
This would show that the $form variable contains the following array:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374array(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"    ["imageUrl"]=>    string(0) ""  }  ["fields"]=>  array(0) {  }  ["version"]=>  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 submission from {form_title}"      ["message"]=>      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) "message"      ["message"]=>      string(64) "Thanks for contacting us! We will get in touch with you shortly."      ["url"]=>      string(0) ""      ["pageId"]=>      string(0) ""      ["queryString"]=>      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, make the appropriate changes, then update that form. Your code would look something like this:
12345$form_id = '1';$form = GFAPI::get_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 getting the form object for that form. Next, we define the new title for the form. We now will take 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.
Getting and Manipulating Entries
In addition to obtaining and editing forms, you may also do the same with entries when using the Gravity Forms API.
Getting All Entries
1234
In the above example, we are simply getting up to 20 entries for the specified form and assigning the array to the $entry variable. If you want to display the contents of this, you may do the following:
1var_dump( $entry );
Which would then output something similar to the following:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263array(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_page=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_agent"]=>    string(82) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/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) ""  }}
If you want to return more than twenty entries you would need to specify a larger page_size in the $paging parameter, see the following for more detail: GFAPI::get_entries
Getting a Single Entry
Just as it is possible to get all entries using the Gravity Forms API, you may also obtain information on a single entry.
1234
In the example above, the results are stored within the $entry entry variable. If you would like to see the object contained within it, you may run the following:
1var_dump( $entry );
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960array(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_page=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_agent"]=>  string(82) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/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 get any information that you want about the specified entry.
Update an Entry
To update an entry, you will need to first get that entry, replace your desired information, and then update it with the new information. This is done like so:
1234567
Within the code example above, we are defining the $entry_id as 1, and passing it to get_entry to get the entry with ID 1. Next, we are taking the results and modifying a part of the array. Finally, we are passing the modified array to update_entry, then returning the result.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注