gform_preview_init

gform_preview_init

DescriptionUsageParametersExamplesSinceSource Code

Description
The 「gform_preview_init」 action is triggered when the form preview page loads, allowing further actions to be done.
Usage
1add_action( 'gform_preview_init', 'my_function' );

Parameters
This hook has no parameters.

Examples
1234function my_function() {    // Do something here}add_action( 'gform_preview_init', 'my_function' );
Since
This action hook was added in Gravity Forms 2.5
Source Code
This action hook is located in preview.php.

gform_preview_header

gform_preview_header

DescriptionUsageParametersExamplesSinceSource Code

Description
The 「gform_preview_header」 action is triggered when the header of the form preview page loads, allowing further actions to be done.
Usage
add_action( 'gform_preview_header', 'my_function', 10, 1 );

Parameters

$form_id int
The ID of the current form being previewed.

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

Since
This action hook was added in Gravity Forms 2.4.19
Source Code
This action hook is located in preview.php.

gform_preview_form_link

gform_preview_form_link

DescriptionUsageParametersExamples1. Change link textPlacementSinceSource Code

Description

The gform_preview_form_link filter allows the admin form preview link to be customized.

Usage

The filter which would run for all forms would be used like so:

add_filter( 'gform_preview_form_link', 'your_function_name' );

Parameters

$preview_link stringThe Form preview link HTML.

Examples

1. Change link text

add_filter( 'gform_preview_form_link', function ( $preview_link ) {
return str_replace( esc_html__( 'Preview', 'gravityforms' ), 'Your custom link text here', $preview_link );
} );

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 GFCommon::get_preview_link() in common.php.

gform_preview_footer

gform_preview_footer

DescriptionUsageParametersExamplesSource Code

Description
The 「gform_preview_footer」 action is triggered when the footer of the form preview page loads, allowing further actions to be done.
Usage
add_action( 'gform_preview_footer', 'my_function', 10, 1 );

Parameters

$id int
The ID of the current form being previewed.

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

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

gform_preview_body_open

gform_preview_body_open

DescriptionUsageParametersExamplesSinceSource Code

Description
The 「gform_preview_body_open」 action is triggered when the body of the form preview page loads, allowing further actions to be done.
Usage
1add_action( 'gform_preview_body_open', 'my_function', 10, 1 );

Parameters

$form_id int
The ID of the current form being previewed.

Examples
1234function my_function() {    //Do something here}add_action( 'gform_preview_body_open', 'my_function', 10, 1 );
Since
This action hook was added in Gravity Forms 2.4.19
Source Code
This action hook is located in preview.php.

gform_predefined_choices

gform_predefined_choices

DescriptionUsageParametersExamples1. Add a new choice2. Add a new choice with values3. Add U.S. State Codes as Choice ValuesSource Code

Description
This filter is executed when the form editor is loaded, before creating the list of predefined choices for the selection fields (Checkboxes, Multiple Choice and Drop Down). This hook can be used to add new predefined choices as well as deleting existing ones.
Usage
This would apply the function to all forms.
1add_filter( 'gform_predefined_choices', 'your_function_name' );
To limit the scope of the function to a specific form simply append the form id to the end of the hook name. (format: gform_predefined_choices_FORMID)
1add_filter( 'gform_predefined_choices_5', 'your_function_name' );

Parameters

$choices array
An array with the existing predefined choices to be filtered. It is an associative array where the key is the title and the value is an array containing the choices.
1$choices['My Favorite Food'] = array( 'Fruit', 'Hamburger', 'Beans' );

Examples
1. Add a new choice
This example adds a new predefined choice to the end of the list.
12345add_filter( 'gform_predefined_choices', 'add_predefined_choice' );function add_predefined_choice( $choices ) {   $choices['My New Choice'] = array( 'Choice 1', 'Choice 2', 'Choice 3' );   return $choices;}
2. Add a new choice with values
This example adds a new predefined choice incorporating choices that have a value that is different than the choice label.
12345add_filter( 'gform_predefined_choices', 'add_predefined_choice' );function add_predefined_choice( $choices ) {   $choices['My New Choice'] = array( 'Choice 1|One', 'Choice 2|Two', 'Choice 3|Three' );   return $choices;}
3. Add U.S. State Codes as Choice Values
This example shows how you can update the choices for the U.S. states to include the state code as the choice value.
12345678add_filter( 'gform_predefined_choices', 'update_us_states' );function update_us_states( $choices ) {    foreach ( $choices['U.S. States'] as &$state ) {        $state .= '|' . GF_Fields::get( 'address' )->get_us_state_code( $state );    }     return $choices;}
Source Code
This filter is located in form_detail.php.

gform_pre_validation

gform_pre_validation

DescriptionUsageParametersExamples1. Conditionally Required Field2. Populate Choices – Drop Down3. Dynamically add a reCAPTCHA field to all formsPlacementSource CodeSince

Description
This filter can be used to manipulate the Form Object used during the form validation process.
Usage
The following would apply to all forms.
add_filter( 'gform_pre_validation', '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_validation_FORMID)
add_filter( 'gform_pre_validation_6', 'your_function_name' );

Parameters

$form Form Object
The current form to be filtered.

Examples
1. Conditionally Required Field
The following example shows how you can conditionally require a field based on the value of another field.
add_filter( 'gform_pre_render', 'gw_conditional_requirement' );
add_filter( 'gform_pre_validation', 'gw_conditional_requirement' );
function gw_conditional_requirement( $form ) {
$value = rgpost( 'input_2' );
if ( $value == 'no' ) {
return $form;
}

foreach ( $form['fields'] as &$field ) {
if ( $field->id == 1 ) {
$field->isRequired = true;
}
}
return $form;
}

2. Populate Choices – Drop Down
This example dynamically populates a drop down field with posts that are in the Business category.
add_filter( 'gform_pre_validation', 'populate_dropdown' );
function populate_dropdown( $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 drop down item array.
$items = array();

//Adding initial blank value.
$items[] = array( 'text' => '', 'value' => '' );

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

3. Dynamically add a reCAPTCHA field to all forms
The following example shows how you can dynamically add reCAPTCHA to all your forms.
add_filter( 'gform_pre_render', 'add_captcha_field' );
add_filter( 'gform_pre_validation', 'add_captcha_field' );
function add_captcha_field( $form ) {
if ( empty( $form['fields'] ) || ! is_array( $form['fields'] ) ) {
return $form;
}

$page_number = 1;
$has_captcha_field = false;

foreach ( $form['fields'] as $field ) {
if ( $field->type == 'captcha' ) {
$has_captcha_field = true;
}

if ( $field->type == 'page' ) {
$page_number ++;
}
}

if ( ! $has_captcha_field ) {
$form['fields'][] = GF_Fields::create( array(
'type' => 'captcha',
'id' => GFFormsModel::get_next_field_id( $form['fields'] ),
'label' => 'Captcha',
'displayOnly' => true,
'formId' => $form['id'],
'pageNumber' => $page_number,
'visibility' => 'visible',
) );
}

return $form;
}

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
$form = gf_apply_filters( 'gform_pre_validation', $form['id'], $form );

This filter is located in GFFormDisplay::validate() in form_display.php.
Since
This filter was added in Gravity Forms 1.9.

gform_pre_submission

gform_pre_submission

DescriptionUsageParametersExamples1. Populate a field2. Populate a field using the value from another field3. Populate a field with the agePlacementSource Code

Description
This action hook is executed after form validation, but before any notifications are sent and the entry is stored. This action can be used to modify the posted values prior to creating the entry.
Usage
Applies to all forms.
add_action( 'gform_pre_submission', 'pre_submission' );

Applies to a specific form. In this case, form id 5.
add_action( 'gform_pre_submission_5', 'pre_submission' );

Parameters

$form Form Object
The current form.

Examples
1. Populate a field
This example changes the post variable for field 14:
add_action( 'gform_pre_submission', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
$_POST['input_14'] = 'new value for field 14';
}

2. Populate a field using the value from another field
This example changes the post variable for field 14 to the value of field 5 for a form with id 1:
// Change 1 on the following to your form id number.
add_action( 'gform_pre_submission_1', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
// Change 14 and 5 to the id number of your fields.
$_POST['input_14'] = rgpost( 'input_5' );
}

3. Populate a field with the age
add_action( 'gform_pre_submission_5', function ( $form ) {
// Get the date field.
$date_field_id = '10';
$date_field = GFAPI::get_field( $form, $date_field_id );

// Get the date field value.
$value = $date_field->get_value_submission( array() );
$date_value = GFFormsModel::prepare_date( $date_field->dateFormat, $value );

// Get the DateTime object representing the difference between the date field value and today.
$today = new DateTime();
$diff = $today->diff( new DateTime( $date_value ) );

// Populate field 11 with the age.
$_POST['input_11'] = $diff->y;
} );

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This action hook is located in GFFormDisplay::process_form() in form_display.php.

gform_pre_submission_filter

gform_pre_submission_filter

DescriptionUsageParametersExamplesPlacementSource Code

Description
This filter is executed after form validation, but before any notifications are sent and the entry is stored. This filter can be used to manipulate the Form Object prior to sending notifications.
Usage
The following would apply the function to all forms.
1add_filter( 'gform_pre_submission_filter', 'pre_submission_filter' );
To target a specific form append the form id to the hook name. (format: gform_pre_submission_filter_FORMID)
1add_filter( 'gform_pre_submission_filter_5', 'pre_submission_filter' );

Parameters

$form Form Object
The form currently being processed.

Examples
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 retrieved        // more info: [http://codex.wordpress.org/Template_Tags/get_posts](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;}
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GFFormDisplay::process_form() in form_display.php

gform_pre_send_email

gform_pre_send_email

DescriptionUsageParametersExamples1. Abort emails.2. Update email meta.3. Wrap message in HTML tags.4. Copy To to Bcc.5. Change default font sizes for a notification using {all_fields}PlacementSource Code

Description
Use this filter to modify the email before a notification has been sent. You may also use this to prevent an email from being sent.
Usage
add_filter( 'gform_pre_send_email', 'your_function_name', 10, 4 );

Parameters

$email array
Array
(
[to] => [email protected]
[subject] => New submission from A
[message] => This is the email body.
[headers] => Array
(
[From] => From: "[email protected]"
[Content-type] => Content-type: text/html; charset=UTF-8
)

[attachments] => Array
(
)

[abort_email] => FALSE
)

$message_format string
The message format, html or text.

$notification array
An array of properties which make up a notification object. See Notifications Object for possible properties. Available from 1.9.15.6.

$entry array
The current Entry Object. Available from 2.2.3.8.

Examples
1. Abort emails.
This example prevents the email from being sent.
add_filter( 'gform_pre_send_email', 'before_email' );
function before_email( $email ) {
//cancel sending emails
$email['abort_email'] = true;
return $email;
}

2. Update email meta.
This example updates the to address, subject, and message.
add_filter( 'gform_pre_send_email', 'before_email' );
function before_email( $email ) {
$email['to'] = '[email protected]';
$email['message'] = 'This is my new message.';
$email['subject'] = 'This is a new subject.';
return $email;
}

3. Wrap message in HTML tags.
add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {
if ( $message_format != 'html' ) {
return $email;
}

$email['message'] = '' . $email['message'] . '';

return $email;
}, 10, 2 );

4. Copy To to Bcc.
add_filter( 'gform_pre_send_email', function ( $email, $message_format, $notification ) {
if ( $notification['name'] != 'User Email' ) {
return $email;
}

$email['headers']['Bcc'] = 'Bcc: ' . $email['to'];
$email['to'] = '[email protected]';

return $email;
}, 10, 3 );

5. Change default font sizes for a notification using {all_fields}
add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {
if ( $message_format != 'html' ) {
return $email;
}

// Change default 12px to 18px
$email['message'] = str_replace( 'font-size:12px', 'font-size:18px', $email['message'] );

// Change default 14px to 20px
$email['message'] = str_replace( 'font-size:14px', 'font-size:20px', $email['message'] );

return $email;
}, 10, 2 );

Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This action hook is located in GFCommonn::send_email() in common.php