bookmark_borderGetting Started with the Gravity Forms API (GFAPI)

When looking over all of the documentation for the Gravity Forms API, it can be a quite daunting experience. This is mainly due to the sheer amount of data that can be received and manipulated within the GFAPI class. While the Gravity Forms API is very powerful, it is actually extremely easy to use. In this article, we will get you started in using the Gravity Forms API to access and manage information within Gravity Forms.

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:

<?php
$forms = GFAPI::get_forms();
?>

This will store your results within the $form variable. To view information on your results, you may call something like this:

var_dump( $forms );

If you were to do so, you will see something like 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"
      ["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:

<?php
echo $forms&#91;0&#93;&#91;'title'&#93;;
?>

Getting a Single Form

Getting a single form is just as easy as getting all forms. Below is an example on doing so:

<?php
$form_id = '1';
$form = GFAPI::get_form( $form_id );
?>

To display the results of this, simply 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"
    ["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:

$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

<?php
$form_id = '1';
$entry = GFAPI::get_entries( $form_id );
?>

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:

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_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.

<?php
$entry_id = '1';
$entry = GFAPI::get_entry( $entry_id );
?>

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:

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_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:

<?php
$entry_id = '1';
$entry = GFAPI::get_entry( $entry_id );
$entry&#91;'1'&#93; = '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 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.

Further Information

While we cannot explain every possible scenario, this article should have given you a general idea of how to use the Gravity Forms API. For more information, take a look at the Gravity Forms API documentation.