Modifying Notification to Add Attachments based on Checkbox

I’d like to know if it’s possible to attach multiple files based on items that are checked off in a checkbox field similar to how it is shown for 1 file in the documentation:

I have 5 PDF files that I would like to be added to my notification based on the choices selected. I’ve tried multiple variations of adding to the attachments array, but nothing works.

(Example: 1 & 3 selected = PDFs 1 & 3 are attached • 2 & 4 selected = PDFs 2 & 4 are attached)

Any help would be greatly appreciated.

I was able to figure it out. Hopefully, this well help someone else who might run into any issues. I’ve replaced my data with placeholders.


add_filter( 'gform_notification_1', 'add_attachments_to_notification', 10, 3 ); // REPLACE 1 WITH YOUR FORM ID
function add_attachments_to_notification( $notification, $form, $entry ) {
    
    GFCommon::log_debug( __METHOD__ . '(): Start.' ); // LOG MESSAGE - START
    
	if( $notification['name'] == 'Test Notification' ) { // REPLACE Test Notification WITH THE NAME OF YOUR NOTIFICATION
        
        GFCommon::log_debug( __METHOD__ . '(): Notification name is Test Notification.' ); // LOG MESSAGE - NOTIFICATION NAME

        
        $checkbox_field_id = 76; // REPLACE 76 WITH THE ID OF YOUR CHECKBOX FIELD
		
        // GET THE VALUES OF THE SELECTED CHECKBOXES

        // REPLACE 5 WITH THE NUMBER OF OPTIONS IN YOUR CHECKBOX FIELD
		$checkbox_values = array();
		for ( $i = 1; $i <= 5; $i++ ) {
			$checkbox_values[] = rgar( $entry, $checkbox_field_id . '.' . $i );
		}
		
        // LOG - VERIFY CHECKBOX VALUES

		if ( empty( $checkbox_values ) ) {
			GFCommon::log_debug( __METHOD__ . '(): No checkboxes selected.' );
		} else {
			GFCommon::log_debug( __METHOD__ . '(): Checkbox values: ' . print_r( $checkbox_values, true ) );
		}

        
        // GET THE UPLOAD ROOT FOR WORDPRESS
        $upload = wp_upload_dir();
        $upload_path = $upload['basedir'];
        
        // DEFINE AN ARRAY OF ATTACHMENT FILE PATHS
        $attachments = array(
            'Value1' => $upload_path . '/2023/04/file-1.pdf', 
            'Value2' => $upload_path . '/2023/04/file-2.pdf', 
            'Value3' => $upload_path . '/2023/04/file-3.pdf', 
            'Value4' => $upload_path . '/2023/04/file-4.pdf', 
            'Value5' => $upload_path . '/2023/04/file-5.pdf'
        );
        
        GFCommon::log_debug( __METHOD__ . '(): Attachments: ' . print_r( $attachments, true ) );
        
        // LOOP THROUGH THE ATTACHMENT ARRAY AND ADD THE ATTACHMENTS TO THE NOTIFICATION
        foreach ( $attachments as $value => $path ) {
            
            GFCommon::log_debug( __METHOD__ . '(): Processing attachment: ' . $value );
            
            if ( in_array( $value, $checkbox_values ) ) {
                if ( file_exists( $path ) ) {
                    $notification['attachments'] = rgar( $notification, 'attachments', array() );
                    $notification['attachments'][] = $path;
                } else {
                    GFCommon::log_debug( __METHOD__ . '(): Not attaching; file does not exist.' );
                }
            }
        }
    }
    
    GFCommon::log_debug( __METHOD__ . '(): End.' );
    
    // RETURN ALTERED NOTIFICATION OBJECT
    return $notification;
}
1 Like