The gform_{$SHORT_SLUG}_error action in Gravity Forms is used to handle errors that occur when processing feeds in various Gravity Forms add-ons. It is typically used to perform additional actions when an error occurs, such as sending a notification or logging the error.
Usage
To use the gform_{$SHORT_SLUG}_error action, you add it to your theme’s functions.php file. Here’s how you would use it for the Mailchimp add-on:
add_action( 'gform_mailchimp_error', 'your_function_name', 10, 4 );
You can also target specific form by appending the form id to the hook name, like so:
add_action( 'gform_mailchimp_error_10', 'your_function_name', 10, 4 );
Remember to replace ‘your_function_name’ with the name of your custom function.
Parameters
- $feed (Feed Object): The feed currently being processed.
- $entry (Entry Object): The entry currently being processed.
- $form (Form Object): The form currently being processed.
- $error (string): The error message.
More Information
See Gravity Forms Docs: gform_{$SHORT_SLUG}_error
The hook is located in GFFeedAddOn::add_feed_error() in includes/addon/class-gf-feed-addon.php. This hook was first implemented in Gravity Forms 1.9.11.9.
Examples
Mailchimp API Issue
In this example, we use the gform_mailchimp_error action to trigger a notification if the Mailchimp API could not be initialized.
add_action( 'gform_mailchimp_error', 'send_mailchimp_error_email', 10, 4 );
function send_mailchimp_error_email( $feed, $entry, $form, $error_message ) {
// Send notifications
GFAPI::send_notifications( $form, $entry, 'mailchimp_api_issue' );
}
The ‘mailchimp_api_issue’ notification event is added via the gform_notification_events filter.
Agile CRM Error
Here’s how you might handle errors for the Agile CRM add-on:
add_action( 'gform_agilecrm_error', 'send_agilecrm_error_email', 10, 4 );
function send_agilecrm_error_email( $feed, $entry, $form, $error_message ) {
// Send notifications
GFAPI::send_notifications( $form, $entry, 'agilecrm_api_issue' );
}
In this case, ‘agilecrm_api_issue’ is the notification event added via the gform_notification_events filter.
Form-Specific Error Handling
You can also target a specific form. In the following example, we’re handling errors for form 10 of the Mailchimp add-on:
add_action( 'gform_mailchimp_error_10', 'handle_form10_errors', 10, 4 );
function handle_form10_errors( $feed, $entry, $form, $error_message ) {
// Custom code here
}
Custom Error Message
Suppose you want to customize the error message shown to the user. You can do it like this:
add_action( 'gform_mailchimp_error', 'custom_error_message', 10, 4 );
function custom_error_message( $feed, $entry, $form, $error_message ) {
// Custom error message
$error_message = "There was a problem processing your request. Please try again later.";
return $error_message;
}
Logging Errors
Finally, you might want to log errors for further investigation. Here’s how you might do that:
add_action( 'gform_mailchimp_error', 'log_mailchimp_errors', 10, 4 );
function log_mailchimp_errors( $feed, $entry, $form, $error_message ) {
// Log error
error_log("Error processing Mailchimp feed: $error_message");
}
In this case, the error message will be written to your PHP error log. Be sure to replace ‘mailchimp’ with the appropriate add-on slug for your use case.