gform_pre_replace_merge_tags

gform_pre_replace_merge_tags

DescriptionUsageParametersExamples1. Replace {form_title}Source CodeSince

Description
Use this filter to replace default merge tags before they are replaced by GFCommon::replace_variables().
Usage
1add_filter( 'gform_pre_replace_merge_tags', 'replace_custom_merge_tags', 10, 7 );

Parameters

$text string
The current text in which merge tags are being replaced.

$form Form Object
The current form.

$entry Entry Object
The current entry.

$url_encode boolean
Whether or not to encode any URLs found in the replaced value.

$esc_html boolean
Whether or not to encode HTML found in the replaced value.

$nl2br boolean
Whether or not to convert newlines to break tags.

$format string
Determines how the value should be formatted. Default is html.

Examples
1. Replace {form_title}
This example shows how you can replace the {form_title} merge tag with your own custom text.
123456789add_filter( 'gform_pre_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {    $merge_tag = '{form_title}';     if ( strpos( $text, $merge_tag ) === false || empty( $form ) ) {        return $text;    }     return str_replace( $merge_tag, 'Your Custom Text', $text );}, 10, 7 );
Source Code
1apply_filters( 'gform_pre_replace_merge_tags', $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format )
This filter is located in GFCommon::replace_variables() in common.php.
Since
This filter was added in Gravity Forms 1.9.6.

gform_pre_render

gform_pre_render

DescriptionUsageParametersExamples1. Populate Choices2. Populate Choices – Checkboxes3. Populate Field With Values From Earlier Page4. Configure Conditional Logic5. Populate coupon fieldPlacementSource Code

Description
The gform_pre_render filter is executed before the form is displayed and can be used to manipulate the Form Object prior to rendering the form.
This filter should be used in conjunction with the gform_pre_validation, gform_pre_submission_filter, and gform_admin_pre_render filters to update the Form Object to be able to use those values elsewhere (merge tags in the confirmation and notification, for example).

IMPORTANT: For obvious reasons, this filter can』t run when the page where your form is embedded is cached. This is not a Gravity Forms limitation but a consequence of using caching.

Usage
The following would apply to all forms.
1add_filter( 'gform_pre_render', 'your_function_name' );
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_render_FORMID)
1add_filter( 'gform_pre_render_6', 'your_function_name' );

Parameters

$form Form Object
The current form to be filtered.

$ajax bool
Is AJAX enabled.

$field_values array
An array of dynamic population parameter keys with their corresponding values to be populated.

Examples
1. Populate Choices
This example dynamically populates a drop down, radio button or multi-select field with posts that are in the Business category.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546add_filter( 'gform_pre_render', 'populate_choices' ); //Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.add_filter( 'gform_pre_validation', 'populate_choices' ); //Note: when changing choice values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.add_filter( 'gform_admin_pre_render', 'populate_choices' ); //Note: this will allow for the labels to be used during the submission process in case values are enabledadd_filter( 'gform_pre_submission_filter', 'populate_choices' );function populate_choices( $form ) {     //only populating drop down for form id 5    if ( $form['id'] != 5 ) {       return $form;    }     //Reading posts for "Business" category;    $posts = get_posts( 'category=' . get_cat_ID( 'Business' ) );     //Creating item array.    $items = array();     //Add a placeholder to field id 8, is not used with multi-select or radio, will overwrite placeholder set in form editor.    //Replace 8 with your actual field id.    $fields = $form['fields'];    foreach( $form['fields'] as &$field ) {      if ( $field->id == 8 ) {        $field->placeholder = 'This is my placeholder';      }    }     //Adding post titles to the items array    foreach ( $posts as $post ) {        $items[] = array( 'value' => $post->post_title, 'text' => $post->post_title );    }     //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.    foreach ( $form['fields'] as &$field ) {        if ( $field->id == 8 ) {            $field->choices = $items;        }    }     return $form;}
2. Populate Choices – Checkboxes
The following example dynamically populates a checkbox field with a list of published posts
12345678910111213141516171819202122232425262728293031323334353637383940//NOTE: update the '221' to the ID of your formadd_filter( 'gform_pre_render_221', 'populate_checkbox' );add_filter( 'gform_pre_validation_221', 'populate_checkbox' );add_filter( 'gform_pre_submission_filter_221', 'populate_checkbox' );add_filter( 'gform_admin_pre_render_221', 'populate_checkbox' );function populate_checkbox( $form ) {     foreach( $form['fields'] as &$field )  {         //NOTE: replace 3 with your checkbox field id        $field_id = 3;        if ( $field->id != $field_id ) {            continue;        }         // you can add additional parameters here to alter the posts that are retreieved        // more info: http://codex.wordpress.org/Template_Tags/get_posts        $posts = get_posts( 'numberposts=-1&post_status=publish' );         $input_id = 1;        foreach( $posts as $post ) {             //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)            if ( $input_id % 10 == 0 ) {                $input_id++;            }             $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );            $inputs[] = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );             $input_id++;        }         $field->choices = $choices;        $field->inputs = $inputs;     }     return $form;}
3. Populate Field With Values From Earlier Page
This example is for a two-page form. The data submitted from the first page is displayed on the second page as a preview. The second page has only one field, an html field that will be populated with the data from the first page.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677add_filter( 'gform_pre_render_81', 'populate_html' );function populate_html( $form ) {    //this is a 2-page form with the data from page one being displayed in an html field on page 2    $current_page = GFFormDisplay::get_current_page( $form['id'] );    $html_content = "The information you have submitted is as follows:

    ";    if ( $current_page == 2 ) {        foreach ( $form['fields'] as &$field ) {            //gather form data to save into html field (id 6 on my form), exclude page break            if ( $field->id != 6 && $field->type != 'page' ) {                //see if this is a complex field (will have inputs)                if ( is_array( $field->inputs ) ) {                    //this is a complex fieldset (name, adress, etc.) - get individual field info                    //get field's label and put individual input information in a comma-delimited list                    $html_content .= '

  • ' .$field->label . ' - ';                    $num_in_array = count( $field->inputs );                    $counter = 0;                    foreach ( $field->inputs as $input ) {                        $counter++;                        //get name of individual field, replace period with underscore when pulling from post                        $input_name = 'input_' . str_replace( '.', '_', $input['id'] );                        $value = rgpost( $input_name );                        $html_content .= $input['label'] . ': ' . $value;                        if ( $counter < $num_in_array ) {                            $html_content .= ', ';                        }                    }                    $html_content .= "
  • ";                } else {                    //this can be changed to be a switch statement if you need to handle each field type differently                    //get the filename of file uploaded or post image uploaded                    if ( $field->type == 'fileupload' || $field->type == 'post_image' ) {                        $input_name = 'input_' . $field->id;                        //before final submission, the image is stored in a temporary directory                        //if displaying image in the html, point the img tag to the temporary location                        $temp_filename = RGFormsModel::get_temp_filename( $form['id'], $input_name );                        $uploaded_name = $temp_filename['uploaded_filename'];                        $temp_location = RGFormsModel::get_upload_url( $form['id'] ) . '/tmp/' . $temp_filename['temp_filename'];                        if ( !empty( $uploaded_name ) ) {                            $html_content .= '

  • ' . $field->label . ': ' . $uploaded_name . "
  • ";                        }                    } else {                        //get the label and then get the posted data for the field (this works for simple fields only - not the field groups like name and address)                        $field_data = rgpost('input_' . $field->id );                        if ( is_array( $field_data ) ){                            //if data is an array, get individual input info                            $html_content .= '

  • ' . $field->label . ': ';                            $num_in_array = count( $field_data );                            $counter = 0;                            foreach ( $field_data as $data ) {                                $counter++;                                $html_content .= print_r( $data, true );                                if ( $counter < $num_in_array ) {                                    $html_content .= ', ';                                }                            }                            $html_content .= '
  • ';                        }                        else {                            $html_content .= '

  • ' . $field->label . ': ' . $field_data . '
  • ';                        }                    }                }            }        }        $html_content .= '

';        //loop back through form fields to get html field (id 6 on my form) that we are populating with the data gathered above        foreach( $form['fields'] as &$field ) {            //get html field            if ( $field->id == 6 ) {                //set the field content to the html                $field->content = $html_content;            }        }    }    //return altered form so changes are displayed    return $form;}
4. Configure Conditional Logic
This example dynamically adds conditional logic to a form field (field 2). The conditional logic will display field 2 only if the selection from field id 1 is First Choice.
12345678910111213141516171819202122add_filter( 'gform_pre_render', 'set_conditional_logic' );add_filter( 'gform_pre_process', 'set_conditional_logic' );function set_conditional_logic( $form ) {     //Set conditional logic only for form 14    if ( $form['id'] !== 14 ) {        return $form;    }     foreach ( $form['fields'] as &$field ) {        if ( $field->id == 2 ) {            $field->conditionalLogic =                array(                    'actionType' => 'show',                    'logicType' => 'all',                    'rules' =>                        array( array( 'fieldId' => 1, 'operator' => 'is', 'value' => 'First Choice' ) )                );        }    }    return $form;}
5. Populate coupon field
The following example shows how the coupon field can be pre populated with one or more coupon codes.
1234567891011121314151617181920212223242526add_filter( 'gform_pre_render_160', function ( $form ) {    $form_id = $form['id'];    if ( empty( $_POST[ 'is_submit_' . $form_id ] ) && function_exists( 'gf_coupons' ) ) {        // Define a comma separated string of coupon codes to apply.        $coupon_codes = '25OFF';         // Get the specified coupons.        $coupons = gf_coupons()->get_coupons_by_codes( $coupon_codes, $form );         // Initialize the $coupon_details array.        $coupon_details = array();         // Add the coupons to the $coupon_details array.        foreach ( $coupons as $coupon ) {            $coupon_details[ $coupon['code'] ] = $coupon;        }         // Add the coupon codes to the hidden coupon codes input for field 3.        $_POST['input_3'] = $coupon_codes;         // Add the coupon details to the hidden coupon details input for the form.        $_POST[ 'gf_coupons_' . $form_id ] = json_encode( $coupon_details );    }     return $form;} );
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GFFormDisplay::get_form() in form_display.php

gform_pre_process

gform_pre_process

DescriptionUsageParametersExamples1. Change Confirmations2. Change Notifications3. Prevent Form Submission4. Use a form field as email for the Save and Continue confirmation5. Save file for base64 data URI6. Reverse order of multi-file uploads7. Disable Partial Entry saving when moving between form pagesPlacementSinceSource Code

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

$form Form Object
The form object.

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

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.

gform_pre_print_scripts

gform_pre_print_scripts

DescriptionUsageParametersExamples1. Print custom scriptPlacementSinceSource Code

Description
The gform_pre_print_scripts action hook is executed just before the scripts are printed to the page for the form widget or when a form embed is processed in a custom location using GFCommon::gform_do_shortcode().
Usage
add_action( 'gform_pre_print_scripts', 'your_function_name', 10, 2 );

You can also specify this per form by adding the form id after the hook name.
add_action( 'gform_pre_print_scripts_6', 'your_function_name', 10, 2 );

Parameters

$form Form Object
The current form object.

$is_ajax bool
Indicates if the form is configured to be submitted via AJAX.

Examples
1. Print custom script
This example prints a custom script for all AJAX enabled forms.
add_action( 'gform_pre_print_scripts', 'print_custom_script', 10, 2 );
function print_custom_script( $form, $is_ajax ) {
if ( $is_ajax ) {
wp_enqueue_script( 'custom_script', 'path/file.js' );
wp_print_scripts( 'custom_script' );
}
}

Placement
This code should be placed in the functions.php file of your active theme or a custom functions plugin.
Since
This filter was added in Gravity Forms v2.5.
Source Code
This filter is located in GFFormDisplay::print_form_scripts() in form_display.php.

gform_pre_notification_save

gform_pre_notification_save

DescriptionUsageParametersExamplesSource Code

Description
Modify the notification object before it is saved to the database. This is particularly useful when saving custom notification settings to the notification object.
Usage
The following would apply to all forms.
1add_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)
1add_filter( 'gform_pre_notification_save_5', 'your_function_name', 10, 2 );

Parameters

$notification array
An array of properties which 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.

Examples
This example demonstrates how you can add the value entered via our gform_notification_ui_settings to the notification object before it is saved to the database. Use with the gform_notification_ui_settings hook to display your custom settings UI.
If your UI settings have a 「name」 attribute, they will be submitted along with the rest of the default notification settings. We can then retrieve our custom value from the $_POST using the Gravity Forms helper function rgpost().
12345add_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;}
Source Code
This filter is located in GFNotification::notification_edit_page() in notification.php

gform_pre_notification_deleted

gform_pre_notification_deleted

DescriptionUsageParametersExamplesSource Code

Description
Triggered before a notification is deleted.
Usage
add_action( 'gform_pre_notification_deleted', 'my_function', 10, 2 );

Parameters

$notification_id int
The ID of the notification being deleted.

$form array
The form object.

Examples
function my_function() {
//Do something here
}
add_action( 'gform_pre_notification_deleted', 'my_function', 10, 2 );

Source Code
This action hook is located in notification.php.

gform_pre_notification_deactivated

gform_pre_notification_deactivated

DescriptionUsageParametersExamplesSource Code

Description
The 「gform_pre_notification_deactivated」 action in Gravity Forms is triggered before a notification is deactivated, allowing further actions to be performed.
Usage
1add_action( 'gform_pre_notification_deactivated', 'my_function', 10, 2 );

Parameters

$notification_id int
The ID of the notification being deactivated.

$form array
The form object.

Examples
1234function my_function() {    //Do something here}add_action( 'gform_pre_notification_deactivated', 'my_function', 10, 2 );
Source Code
This action hook is located in forms_model.php.

gform_pre_notification_activated

gform_pre_notification_activated

DescriptionUsageParametersExamplesSource Code

Description
The 「gform_pre_notification_activated」 action is triggered before a notification is activated, allowing further actions to be performed.
Usage
add_action( 'gform_pre_notification_activated', 'my_function', 10, 2 );

Parameters

$notification_id int
The ID of the notification being activated.

$form array
The form object.

Examples
function my_function() {
//Do something here
}
add_action( 'gform_pre_notification_activated', 'my_function', 10, 2 );

Source Code
This action hook is located in forms_model.php.

gform_pre_note_deleted

gform_pre_note_deleted

DescriptionUsageParametersExamplesSource Code

Description
The 「gform_pre_note_deleted」 action in Gravity Forms is triggered before a note is deleted, allowing further actions to be performed.
Usage
1add_action( 'gform_pre_note_deleted', 'my_function', 10, 2 );

Parameters

$note_id int
The ID of the note being deleted.

$entry_id int
The ID entry that the note is being deleted from.

Examples
1234function my_function() {    //Do something here}add_action( 'gform_pre_note_deleted', 'my_function', 10, 2 );
Source Code
This action hook is located in forms_model.php.

gform_pre_form_settings_save

gform_pre_form_settings_save

DescriptionUsageParametersExamplesSource Code

Description
Modify the form object before saving on the Form Settings page. Useful for saving custom settings added via the gform_form_settings filter.
Usage
1add_filter( 'gform_pre_form_settings_save', 'your_function_name' );

Parameters

$form Form Object
The current form object being edited.

Examples
This example demonstrates how you can save custom settings added via the gform_form_settings filter to the form object using the gform_pre_form_settings_save filter.
123456789101112131415161718// add a custom form settingadd_filter( 'gform_form_settings', 'my_custom_form_setting', 10, 2 );function my_custom_form_setting( $settings, $form ) {    $settings['Form Basics']['my_custom_setting'] = '        

            

            

        

';     return $settings;} // save your custom form settingadd_filter( 'gform_pre_form_settings_save', 'save_my_custom_form_setting' );function save_my_custom_form_setting( $form ) {    $form['my_custom_setting'] = rgpost( 'my_custom_setting' );    return $form;}
Source Code
This filter is located in GFFormSettings::form_settings_ui() in form_settings.php