Using Gravity Forms ‘gform_pre_notification_save PHP filter

The gform_pre_notification_save Gravity Forms PHP filter allows you to modify the notification object before it is saved to the database. This is especially useful for saving custom notification settings to the notification object.

Usage

A generic example of how to use the filter, including a custom function:

add_filter( 'gform_pre_notification_save', 'your_function_name', 10, 2 );

To limit the scope of your function to a specific form, append the form id to the end of the hook name (format: gform_pre_notification_save_FORMID):

add_filter( 'gform_pre_notification_save_5', 'your_function_name', 10, 2 );

Parameters

  • $notification (array): An array of properties that make up the notification object to be saved. See Notifications Object for default properties.
  • $form (Form Object): The current form object to which the notification being saved belongs.

More information

See Gravity Forms Docs: gform_pre_notification_save

Examples

Add a custom setting to the notification object

This example demonstrates how to add a custom setting to the notification object before it is saved to the database. It uses the rgpost() helper function from Gravity Forms to retrieve the custom value from the $_POST.

add_filter( 'gform_pre_notification_save', 'my_custom_notification_save', 10, 2 );
function my_custom_notification_save( $notification, $form ) {
    $notification['my_custom_setting'] = rgpost( 'my_custom_setting' );
    return $notification;
}

Set a custom email subject based on form ID

This example sets a custom email subject for the notification based on the form ID.

add_filter( 'gform_pre_notification_save', 'custom_email_subject', 10, 2 );
function custom_email_subject( $notification, $form ) {
    if ( $form['id'] == 1 ) {
        $notification['subject'] = 'Form 1 Submission';
    } elseif ( $form['id'] == 2 ) {
        $notification['subject'] = 'Form 2 Submission';
    }
    return $notification;
}

Append additional information to the notification message

This example appends extra information to the notification message.

add_filter( 'gform_pre_notification_save', 'append_additional_info', 10, 2 );
function append_additional_info( $notification, $form ) {
    $additional_info = 'Extra information';
    $notification['message'] .= "\n\n" . $additional_info;
    return $notification;
}

Conditionally disable notifications based on a field value

This example disables notifications if a specific field value is present in the form submission.

add_filter( 'gform_pre_notification_save', 'conditionally_disable_notifications', 10, 2 );
function conditionally_disable_notifications( $notification, $form ) {
    $disable_field_id = 10;
    $disable_value = 'disable';

    foreach ( $form['fields'] as $field ) {
        if ( $field->id == $disable_field_id && $field->value == $disable_value ) {
            $notification['isActive'] = false;
            break;
        }
    }
    return $notification;
}