icon

Using the gform_after_submission hook to Integrate Gravity Forms with Zendesk

  • category

    WordPress

Introduction

Gravity Forms is one of the most popular WordPress plugins for creating forms. Its robust API allows developers to extend its functionality through custom hooks and actions. One such hook, gform_after_submission, is particularly useful for performing actions after a form submission is complete.

What is gform_after_submission?

The gform_after_submission hook fires after a form is successfully submitted. It allows developers to access the submitted form data and perform custom actions, such as sending notifications, triggering API calls, or storing additional data.

Syntax:

add_action( 'gform_after_submission', 'your_function_name', 10, 2 );

Parameters:

- $entry: Contains the submitted form data.

- $form: Contains the form structure and metadata.

Using the Hook to Send Data to Zendesk

The following code demonstrates how to use gform_after_submission to send form data to Zendesk's API.

add_action( 'gform_after_submission', function ( $entry, $form ) { // Log that the hook was triggered error_log( 'gform_after_submission triggered' ); // Extract form field values from the entry $name = rgar( $entry, '1' ); // Replace '1' with your field ID for 'name' $email = rgar( $entry, '4' ); // Replace '4' with your field ID for 'email' $subject = rgar( $entry, '7' ); // Replace '7' with your field ID for 'subject' $body = rgar( $entry, '8' ); // Replace '8' with your field ID for 'body' $custom_field_value = rgar( $entry, '5' ); // Replace '5' with your custom field ID // Log extracted values for debugging error_log( 'Name: ' . $name ); error_log( 'Email: ' . $email ); error_log( 'Subject: ' . $subject ); error_log( 'Body: ' . $body ); error_log( 'Custom Field Value: ' . $custom_field_value ); // Construct the custom JSON structure for Zendesk $zendesk_request = [ "request" => [ "requester" => [ "name" => $name, "email" => $email, ], "subject" => $subject, "comment" => [ "body" => $body, ], "custom_fields" => [ [ "id" => 27917443967127, // Replace with the actual custom field ID "value" => $custom_field_value, ], ], ], ]; // Log the JSON payload for debugging error_log( 'Zendesk Payload: ' . json_encode( $zendesk_request ) ); // Send a manual request to Zendesk for debugging $response = wp_remote_post( 'https://your-zendesk-endpoint.com/api/v2/tickets', [ 'headers' => [ 'Authorization' => 'Bearer your_api_token', // Replace with your actual API token 'Content-Type' => 'application/json', ], 'body' => json_encode( $zendesk_request ), ] ); // Log the response for debugging if ( is_wp_error( $response ) ) { error_log( 'Zendesk API Error: ' . $response->get_error_message() ); } else { error_log( 'Zendesk API Response: ' . print_r( $response, true ) ); } }, 10, 2 );

Detailed Breakdown

1. Triggering the Hook: The add_action function registers a callback function to the gform_after_submission hook. When a form is submitted, this callback is executed.

2. Extracting Field Values: Gravity Forms stores submitted data in the $entry parameter. The rgar function is used to retrieve values based on field IDs.

3. Constructing the Zendesk API Payload: The Zendesk API expects a specific JSON structure. This is created as a PHP associative array and then converted to JSON using json_encode.

4. Making the API Request: The wp_remote_post function is used to send a POST request to Zendesk’s API endpoint. The API token and other headers are included to authenticate the request.

5. Logging for Debugging: Error logs are added throughout the function to track progress and identify issues. These logs are stored in the wp-content/debug.log file when debugging is enabled.

How to Enable Debugging

To enable debugging in WordPress, add the following lines to your wp-config.php file:

define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false ); // Optional: Prevent errors from displaying on the site

Key Considerations

1. Field IDs: Ensure the field IDs in the rgar function match your form’s actual field IDs.

2. Zendesk API Token: Replace 'your_api_token' with a valid token generated from your Zendesk account.

3. Error Handling: Use is_wp_error to handle API errors gracefully and log them for troubleshooting.

4. Security: Avoid hardcoding sensitive information (e.g., API tokens). Use environment variables or the WordPress settings API for secure storage.

Testing the Integration

1. Submit the Form: Fill out and submit the form.

2. Check Debug Logs: Review the wp-content/debug.log file to verify that the hook was triggered, the data was extracted correctly, and the API request was sent.

3. Verify Zendesk: Log into your Zendesk account and check if a ticket was created successfully.

Conclusion

The gform_after_submission hook is a powerful tool for extending the functionality of Gravity Forms. By integrating it with third-party APIs like Zendesk, you can automate workflows and enhance user experience. With proper logging and error handling, you can build robust integrations that are easy to debug and maintain.