Using Gravity Forms gf_apply_filters() and gf_do_action() in Gravity Forms development
These two Gravity Forms functions are under-documented, but they're very powerful, so we have provided some additional information for Gravity Forms developers!
Using gf_apply_filters
and gf_do_action
These two functions are powerful tools for creating dynamic, context-specific hooks in your Gravity Forms add-ons. They automatically generate multiple hook variations based on form IDs, field IDs, or other modifiers you provide.
What these functions do
Both gf_apply_filters()
and gf_do_action()
create cascading hooks that go from general to specific. For example, if you use a form ID modifier, the functions will trigger both a general hook (affecting all forms) and a form-specific hook (affecting only that form).
This pattern allows developers to:
- Hook into all forms with one callback
- Hook into specific forms with targeted callbacks
- Hook into specific fields within specific forms for precise control
gf_apply_filters()
Use gf_apply_filters()
when you need to filter or modify data and want to provide both general and specific hook points for other developers.
Basic syntax
$filtered_value = gf_apply_filters( $filter_name_or_array, $value, ...$additional_args );
Parameters
$filter
(string|array): The filter name or an array containing the filter name and modifiers$value
(mixed): The value to be filtered...$args
(mixed): Additional arguments to pass to filter callbacks (up to 10 supported)
Simple example
// Apply a general filter
$entry_meta = gf_apply_filters( 'gform_entry_meta', $entry_meta, $form );
This creates one filter: gform_entry_meta
Example with form ID modifier
// Apply both general and form-specific filters
$entry_meta = gf_apply_filters(
array( 'gform_entry_meta', $form['id'] ),
$entry_meta,
$form
);
This creates two filters:
gform_entry_meta
(general - affects all forms)gform_entry_meta_5
(specific - affects only form ID 5)
Example with form and field ID modifiers
// Apply general, form-specific, and field-specific filters
$field_value = gf_apply_filters(
array( 'gform_field_value', $form['id'], $field->id ),
$value,
$entry,
$field
);
This creates three filters:
gform_field_value
(all fields in all forms)gform_field_value_5
(all fields in form 5)gform_field_value_5_3
(only field 3 in form 5)
Hooking into gf_apply_filters
Developers can hook into any of the generated filter variations:
// Hook into all forms
add_filter( 'gform_entry_meta', 'my_general_callback', 10, 2 );
// Hook into specific form
add_filter( 'gform_entry_meta_5', 'my_form_specific_callback', 10, 2 );
// Hook into specific field in specific form
add_filter( 'gform_field_value_5_3', 'my_field_specific_callback', 10, 4 );
function my_field_specific_callback( $value, $entry, $field, $form ) {
// Only runs for field 3 in form 5
if ( $field->type === 'email' ) {
$value = strtolower( $value );
}
return $value;
}
gf_do_action()
Use gf_do_action()
when you want to trigger actions at specific points and provide both general and specific hook points for other developers.
Basic syntax
gf_do_action( $action_name_or_array, ...$args );
Parameters
$action
(string|array): The action name or an array containing the action name and modifiers...$args
(mixed): Arguments to pass to action callbacks (up to 10 supported)
Simple example
// Trigger a general action
gf_do_action( 'gform_after_submission', $entry, $form );
This triggers one action: gform_after_submission
Example with form ID modifier
// Trigger both general and form-specific actions
gf_do_action(
array( 'gform_after_submission', $form['id'] ),
$entry,
$form
);
This triggers two actions:
gform_after_submission
(general - all forms)gform_after_submission_5
(specific - only form ID 5)
Example with form and field ID modifiers
// Trigger general, form-specific, and field-specific actions
gf_do_action(
array( 'gform_field_validation', $form['id'], $field->id ),
$result,
$value,
$form,
$field
);
This triggers three actions:
gform_field_validation
(all fields in all forms)gform_field_validation_5
(all fields in form 5)gform_field_validation_5_3
(only field 3 in form 5)
Hooking into gf_do_action
Developers can hook into any of the generated action variations:
// Hook into all forms
add_action( 'gform_after_submission', 'process_all_submissions', 10, 2 );
// Hook into specific form
add_action( 'gform_after_submission_5', 'process_contact_form', 10, 2 );
// Hook into specific field validation
add_action( 'gform_field_validation_5_3', 'validate_special_email', 10, 5 );
function validate_special_email( $result, $value, $form, $field, $entry ) {
// Only runs for field 3 in form 5
if ( strpos( $value, '@company.com' ) === false ) {
$result['is_valid'] = false;
$result['message'] = 'Please use your company email address.';
}
}
Real-world examples from Gravity Forms
Entry detail page actions
// From entry_detail.php - trigger actions before displaying entry details gf_do_action( array( 'gform_pre_entry_detail', $form['id'] ), $form, $lead );
Developers can hook in like:
// Process all entry detail pages add_action( 'gform_pre_entry_detail', 'log_entry_views', 10, 2 ); // Process only form 5 entry detail pages add_action( 'gform_pre_entry_detail_5', 'special_contact_processing', 10, 2 );
Order summary filtering
// From common.php - filter order summary data $field_data = gf_apply_filters( array( 'gform_order_summary', $form['id'] ), $field_data, $form, $lead, $products, $format );
Developers can filter like:
// Modify all order summaries add_filter( 'gform_order_summary', 'add_tax_line', 10, 6 ); // Modify only form 3 order summaries add_filter( 'gform_order_summary_3', 'add_shipping_discount', 10, 6 );
Field input filtering
// From common.php - filter field input HTML $field_input = gf_apply_filters( array( 'gform_field_input', $form_id, $field->id ), '', $field, $value, $lead_id, $form_id );
Developers can filter like:
// Modify all field inputs add_filter( 'gform_field_input', 'add_custom_attributes', 10, 5 ); // Modify all fields in form 2 add_filter( 'gform_field_input_2', 'add_form_specific_classes', 10, 5 ); // Modify only field 4 in form 2 add_filter( 'gform_field_input_2_4', 'customize_email_field', 10, 5 );