Using Gravity Forms ‘gform_webapi_$endpoint’ PHP action

The gform_webapi_ENDPOINT Gravity Forms PHP/JavaScript action hook is triggered when a specific Gravity Forms Web API endpoint is accessed.

Usage

add_action('gform_webapi_get_forms', 'my_function', 10, 4);

Parameters

  • $id (string) – The ID of the element obtained, e.g., the form ID if accessing a form.
  • $id2 (string) – Defined only if a secondary ID is specified in the request, such as getting a specific field within a specific entry.
  • $format (string) – The format in which the data is returned. By default, returns JSON.
  • $args (array) – Arguments used within the request, such as the offset, page size, and schema.

More information

See Gravity Forms Docs: gform_webapi_ENDPOINT

Examples

Change API response format

Change the API response format to XML when accessing a form:

function change_response_format($id, $id2, $format, $args) {
  // Change the response format to XML
  return 'xml';
}
add_action('gform_webapi_get_forms', 'change_response_format', 10, 4);

Modify form data before output

Modify form data before it is returned by the API:

function modify_form_data($id, $id2, $format, $args) {
  // Retrieve the form
  $form = GFAPI::get_form($id);

  // Modify the form data, e.g., change the form title
  $form['title'] = 'Modified Title';

  // Return the modified form data
  return $form;
}
add_action('gform_webapi_get_forms', 'modify_form_data', 10, 4);

Add a custom field to form data

Add a custom field to form data before it is returned by the API:

function add_custom_field($id, $id2, $format, $args) {
  // Retrieve the form
  $form = GFAPI::get_form($id);

  // Add a custom field to the form data
  $form['custom_field'] = 'Custom Value';

  // Return the modified form data
  return $form;
}
add_action('gform_webapi_get_forms', 'add_custom_field', 10, 4);

Filter forms based on a specific condition

Filter forms based on a specific condition before returning them in the API response:

function filter_forms($id, $id2, $format, $args) {
  // Retrieve all forms
  $forms = GFAPI::get_forms();

  // Filter forms based on a specific condition, e.g., form title
  $filtered_forms = array_filter($forms, function($form) {
    return $form['title'] === 'My Special Form';
  });

  // Return the filtered forms
  return $filtered_forms;
}
add_action('gform_webapi_get_forms', 'filter_forms', 10, 4);

Log API requests

Log all API requests to a specific Gravity Forms endpoint:

function log_api_requests($id, $id2, $format, $args) {
  // Log API request details
  error_log("API Request: Form ID - {$id}, Format - {$format}, Args - " . print_r($args, true));
}
add_action('gform_webapi_get_forms', 'log_api_requests', 10, 4);