Using Gravity Forms ‘gform_pre_process’ PHP filter

The gform_pre_process filter in Gravity Forms allows you to modify the form object before form submission begins processing. This is particularly useful for modifying confirmations or notifications.

On this pageJump to a section

Usage

Apply this filter to all forms using:

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

To target a specific form, append the form id to the hook name like so:

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

Insert your custom code in place of 'your_function_name'.

Parameters

  • $form (Form Object): This is the form object that you can manipulate.

More information

See Gravity Forms Docs: gform_pre_process

Examples

Change Confirmations

If you need to change form confirmations, use the following code:

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;
}

This code loops over all confirmations of the form and changes their name and message.

Change Notifications

To change notifications, you can use similar code:

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;
}

This code alters each notification’s subject and message.

Prevent Form Submission

If you want to prevent form submission based on a condition, you can do so:

add_filter( 'gform_pre_process','change_form', 10, 1 );
function change_form( $form ){
    if ( $form['id'] == 75 ) {
        $form['is_active'] = false;
    }
    return $form;
}

This code prevents form submission if the form’s id is 75.

Use a Form Field as Email for the Save and Continue Confirmation

You can use a form field as the email address for the “Save and Continue” confirmation:

add_action( 'gform_pre_process_2', function ( $form ) {
    if ( rgpost( 'gform_save' ) ) {
        $_POST['gform_resume_email'] = rgpost( 'input_1' );
    }
});

This code sets the ‘gform_resume_email’ post variable to the value of the form field with id 1.

Save File for Base64 Data URI

To save a file from a base64 data URI included in the request, you can use the following code:

add_action( 'gform_pre_process_178', function ( $form ) {
    GFCommon::log_debug( 'gform_pre_process: running' );
   

javascript
// Get 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’ );
$target_dir = GFFormsModel::get_upload_path( $form[‘id’] ) . DIRECTORY_SEPARATOR . ‘tmp’ . DIRECTORY_SEPARATOR;
// Create upload directory if it doesn’t exist
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 );
}
}
// ID of the single file upload field
$upload_field_id = 4;
// File extension
$file_extension = ‘png’;
// Get the file contents from the base64 data URI
$file_contents = base64_decode( preg_replace( ‘#^data:image/\w+;base64,#i’, ”, $base64_string ) );
// Get the temp file name
$temp_filename = sprintf( ‘%s_input_%s.%s’, GFFormsModel::get_form_unique_id( $form[‘id’] ), $upload_field_id, $file_extension );
// Save 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 ) );
// Define the name the file should use when saved to the entry
$uploaded_file_name = ‘testing.png’;
// Add 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 ) );
}
});

This code takes a base64 data URI from an input field, converts it to a file, and saves it in a temporary directory.

Leave a Comment

Your email address will not be published. Required fields are marked *