gravityflow_notification

Description

The gravityflow_notification filter allows the emails to be filtered before they’re sent.

Note: Any changes to the $notification[‘to’] or $notification[‘from’] values must result in only a valid email address being defined for the notification to be successful. If you want to modify to also display name details in those fields, you should use the gform_pre_send_email filter of Gravity Forms.

Parameters

ParameterTypeDetails
$notificationNotificationThe potential notification.
$formFormThe current form array.
$entryEntryThe current entry.
$stepStepThe current step.

Examples

Change notification details on a specific notification type

Set the the notification from address and from name to the current user when the current step is approval and the email is an approval or rejection email.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
add_filter( 'gravityflow_notification', 'sh_gravityflow_notification', 10, 4 );
function sh_gravityflow_notification( $notification, $form, $entry, $step ) {
    if ( $step->get_type() == 'approval' && $step->get_status() !== 'pending' ) {
        $current_user = wp_get_current_user();
        $notification['from']     = $current_user->user_email;
        $notification['fromName'] = $current_user->display_name;
        $notification['subject'] = $current_user->display_name;
     
       // Custom subject
       $notification['subject']  = 'New Status: ' . $status;
 
       // Uncomment these lines to disable autoformatting
       //$notification['disableAutoformat']  = '1';
       //$notification['message_format']  = 'text';
    }
    return $notification;
}

Send a file upload field value as an attachment during an Approval step

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
add_filter( 'gravityflow_notification', 'sh_gravityflow_notification', 10, 4 );
function sh_gravityflow_notification( $notification, $form, $entry, $step ) {
 if ( $step->get_type() == 'approval' && $step->get_status() == 'pending' ) {
      
        $fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
        if ( ! is_array( $fileupload_fields ) )
            {
                 
                return $notification;
            }
        $notification['attachments'] = ( is_array( rgget('attachments', $notification ) ) ) ? rgget( 'attachments', $notification ) : array();
        $upload_root = RGFormsModel::get_upload_root();
        foreach( $fileupload_fields as $field ) {
            $url = $entry[ $field->id ];
            $attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
             
            if ( $attachment ) {
                $notification['attachments'] = $attachment;
            }
        }  
         
    }
    gravity_flow()->log_debug( 'Attachment for entry_id '.$entry['id']. " ".var_export( $notification, true ) );
    return $notification;
  
 }

Exclude specific users (by multi-user field in the form) from receiving notifications for certain step(s).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
add_filter( 'gravityflow_notification', 'sh_gravityflow_notification', 10, 4 );
function sh_gravityflow_notification( $notification, $form, $entry, $step ) {
    // Ensure the filter only applies to specific Step IDs.
    if ( in_array( $step->get_id(), array( '167' ) ) ) {
        return $notification;
    }
 
    // Identify which field IDs should be checked for users to exclude from the notification.
    $exclude_field_ids = array( '2', '3' );
 
    // Ensure the notification recipient is a valid WP user.
    if ( ! isset( $notification['to'] ) || $notification['to'] == '' ) {
        return $notification;
    }
 
    $potential_recipient = get_user_by( 'email', $notification['to'] );
    if ( ! $potential_recipient ) {
        return $notification;
    }
 
    foreach ( $exclude_field_ids as $field_id ) {
 
        // Ensure there is atleast 1 user selected in the exclusion field.
        $exclude_users = json_decode( $entry[ $field_id ], true );
        if ( empty( $exclude_users ) ) {
            break;
        }
 
        if ( in_array( $potential_recipient->ID, $exclude_users ) ) {
            unset( $notification['to'] );
        }
    }
 
    return $notification;
}

Send email only to email assignee and ignore all other assignees

1
2
3
4
5
6
7
8
9
10
11
12
13
14
add_filter( 'gravityflow_notification', 'sb_gravityflow_notification', 10, 4 );
 
 
function sb_gravityflow_notification( $notification, $form, $entry, $step ) {
    // Check if the assignee is NOT an email field assignee.
    if ( strpos( $notification['name'], 'email' ) === false ) {
        // Unset the 'to' field for non-email assignees.
        unset( $notification['to'] );
 
    }
 
    // Return the modified notification.
    return $notification;
}


Prevent Notifications from Being Sent to Non-Email Assignees in Gravity Flow

1
2
3
4
5
6
7
8
9
10
11
12
add_filter( 'gravityflow_notification', 'sb_gravityflow_notification', 10, 4 );
 
function sb_gravityflow_notification( $notification, $form, $entry, $step ) {
    // Check if the notification is not for an email-based assignee.
    if ( strpos( $notification['name'], '_email_' ) === false ) {
        // Prevent the notification from being sent.
        return false;
    }
 
    // Return the notification only for email-based assignees.
    return $notification;
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?