gform_pre_process

Description

Allows filtering the Form Object before the form submission has begun processing.

A good use for this is changing confirmations or notifications.

Usage

The following would apply to all forms:

add_filter( 'gform_pre_process','your_function_name', 10, 1 );

To target a specific form, append the form id to the hook name. (format: gform_pre_process_FORMID)

add_filter( 'gform_pre_process_1','your_function_name', 10, 1 );

Parameters

Examples

1. Change Confirmations

add_filter( 'gform_pre_process','change_confirmations', 10, 1 );
function change_confirmations( $form ){
    $confirmations = $form['confirmations'];
    foreach ( $confirmations as $id => $confirmation )
    {
        $confirmation['name'] = 'Test Confirmation';
        $confirmation['message'] = 'This is a test submission. Please disregard.';
        $new_confirmations[$id] = $confirmation;
    }
 
    $form['confirmations'] = $new_confirmations;
    return $form;
}

2. Change Notifications

add_filter( 'gform_pre_process','change_notifications', 10, 1 );
function change_notifications( $form ){
    $notifications = $form['notifications'];
    foreach ( $notifications as $id => $notification )
    {
        $notification['subject'] = 'Test Notification';
        $notification['message'] = 'This is a test submission. Please disregard.';
        $new_notifications[$id] = $notification;
    }
 
    $form['notifications'] = $new_notifications;
    return $form;
}

3. Prevent Form Submission

add_filter( 'gform_pre_process','change_form', 10, 1 );
function change_form( $form ){
	//prevent the submission from being processed
	if ( $form['id'] == 75 ) {
		$form['is_active'] = false;
	}

	return $form;
}

4. Use a form field as email for the Save and Continue confirmation

// Change 2 in the following line to your form id number
add_action( 'gform_pre_process_2', function ( $form ) {
	if ( rgpost( 'gform_save' ) ) {
		// Get email from field id 1. Change 1 in input_1 to your field id number.
		$_POST['gform_resume_email'] = rgpost( 'input_1' );
	}
} );

5. Save file for base64 data URI

This example is useful for REST API form submissions where you need to create a file from a base64 data URI included the request and save it as the value of a single file upload field.

add_action( 'gform_pre_process_178', function ( $form ) {
    GFCommon::log_debug( 'gform_pre_process: running' );
 
    // Getting the base64 data URI from the input named input_3.
    $base64_string = rgpost( 'input_3' );
 
    if ( ! empty( $base64_string ) ) {
        GFCommon::log_debug( 'gform_pre_process: found string' );
 
        // Creating the upload directory if it doesn't already exist.
        $target_dir = GFFormsModel::get_upload_path( $form['id'] ) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
 
        if ( ! is_dir( $target_dir ) ) {
            GFCommon::log_debug( 'gform_pre_process: creating tmp folder' );
            if ( ! wp_mkdir_p( $target_dir ) ) {
                GFCommon::log_debug( "gform_pre_process: Couldn't create the tmp folder: " . $target_dir );
 
                return;
            } else {
                GFCommon::recursive_add_index_file( $target_dir );
            }
        }
 
        // The ID of the single file upload field.
        $upload_field_id = 4;
 
        // The extension the file will be saved using.
        $file_extension  = 'png';
 
        // Getting the file contents from the base64 data URI
        $file_contents = base64_decode( preg_replace( '#^data:image/\w+;base64,#i', '', $base64_string ) );
 
        // Getting the temp file name that the file upload field expects the file to use.
        $temp_filename = sprintf( '%s_input_%s.%s', GFFormsModel::get_form_unique_id( $form['id'] ), $upload_field_id, $file_extension );
 
        // Saving the temp file.
        $result = file_put_contents( $target_dir . $temp_filename, $file_contents );
        GFCommon::log_debug( 'gform_pre_process: file_put_contents result: ' . var_export( $result, true ) );
 
        // Defining the name the file should use when saved to the entry. 
        $uploaded_file_name = 'testing.png';
 
        // Adding the temp file details to the gform_uploaded_files input which Gravity Forms will access when saving the entry.
        $_POST['gform_uploaded_files'] = json_encode( array( 'input_' . $upload_field_id => $uploaded_file_name ) );
    }
} );

6. Reverse order of multi-file uploads

add_action( 'gform_pre_process', 'reverse_mf_uploads_pre_process' );
function reverse_mf_uploads_pre_process( $form ) {
    $gform_uploaded_files = rgpost( 'gform_uploaded_files' );
    if ( ! empty( $gform_uploaded_files ) ) {
        $files  = json_decode( $gform_uploaded_files, true );
        $fields = GFAPI::get_fields_by_type( $form, array( 'fileupload' ), true );
 
        foreach ( $fields as $field ) {
            $input_name = 'input_' . $field->id;
 
            if ( ! $field->multipleFiles || ! isset( $files[ $input_name ] ) ) {
                continue;
            }
 
            $files[ $input_name ] = array_reverse( $files[ $input_name ] );
        }
 
        $_POST['gform_uploaded_files'] = json_encode( $files );
    }
}

7. Disable Partial Entry saving when moving between form pages

add_filter( 'gform_pre_process', function ( $form ) {
	if ( class_exists( 'GF_Partial_Entries' ) && empty( $_POST['gform_save'] ) ) {
		remove_action( 'gform_post_process', array( GF_Partial_Entries::get_instance(), 'action_gform_post_process' ) );
	}
	return $form;
} );

8. Use the logged in user email address as email for the Save and Continue confirmation

add_action( 'gform_pre_process', function ( $form ) {
	if ( rgpost( 'gform_save' ) && is_user_logged_in() ) {
		$current_user = wp_get_current_user();
		$_POST['gform_resume_email'] = $current_user->user_email;
	}
} );

Placement

This code should be placed in the functions.php file of your active theme.

Since

This filter was added in Gravity Forms version 1.9.15.

Source Code

This filter is located in GFFormDisplay::process_form() in form_display.php.