Description
This filter is executed before creating the field’s content, allowing users to completely modify the way the field is rendered. It can also be used to create custom field types.
It is similar to gform_field_input, but provides more flexibility.
Usage
add_filter( 'gform_field_content', 'my_custom_function', 10, 5 );
// apply to a specific form add_filter( 'gform_field_content_123', 'my_custom_function', 10, 5 );
// apply to a specific form and field add_filter( 'gform_field_content_123_6', 'my_custom_function', 10, 5 );
Parameters
- $field_content string
The field content to be filtered.
-
$field Field Object
The field that this input tag applies to.
-
$value string
The default/initial value that the field should be pre-populated with.
-
$entry_id integer
When executed from the entry detail screen, $lead_id will be populated with the Entry ID. Otherwise, it will be 0.
-
$form_id integer
The current Form ID.
Examples
1. New field type based on field class name
This example creates a sub-section field, replacing fields with a subsection custom CSS class.
add_filter( 'gform_field_content', 'subsection_field', 10, 5 );
function subsection_field( $content, $field, $value, $lead_id, $form_id ) {
if ( $field->cssClass == 'subsection' ) {
if ( RG_CURRENT_VIEW == 'entry' ) {
$mode = empty( $_POST['screen_mode'] ) ? 'view' : $_POST['screen_mode'];
if ( $mode == 'view' ) {
$content = '<tr>
<td colspan="2" class="entry-view-section-break">'. esc_html( GFCommon::get_label( $field ) ) . '</td>
</tr>';
} else {
$content= "<tr valign='top'>
<td class='detail-view'>
<div style='margin-bottom:10px; border-bottom:1px dotted #ccc;'><h2 class='detail_gsection_title'>" . esc_html( GFCommon::get_label( $field ) ) . "</h2></div>
</td>
</tr>";
}
} else {
$delete_field_link = "<a class='field_delete_icon' id='gfield_delete_{$field->id}' title='" . __( 'click to delete this field', 'gravityforms' ) . "' href='javascript:void(0);' onclick='StartDeleteField(this);'>" . __( 'Delete', 'gravityforms' ) . "</a>";
$delete_field_link = apply_filters( 'gform_delete_field_link', $delete_field_link );
//The edit and delete links need to be added to the content (if desired), when using this hook
$admin_buttons = IS_ADMIN ? $delete_field_link . " <a class='field_edit_icon edit_icon_collapsed' title='" . __( 'click to edit this field', 'gravityforms' ) . "'>" . __( 'Edit', 'gravityforms' ) . "</a>" : "";
$content = "{$admin_buttons}<h3 class='subsection_title'>" . $field->label . "</h3>";
}
}
return $content;
}
2. Add Autocomplete Attribute
This example shows how you can edit the input markup and add extra attributes
add_filter( 'gform_field_content', function ( $field_content, $field ) {
if ( $field->type == 'email' ) {
return str_replace( 'type=', "autocomplete='email' type=", $field_content );
}
return $field_content;
}, 10, 2 );
This example shows how you can edit the input markup and add extra attributes to the Credit Card field inputs.
add_filter( 'gform_field_content', function ( $field_content, $field ) {
if ( $field->type == 'creditcard' ) {
$field_content = str_replace( ".1' id", ".1' autocomplete='cc-number' id", $field_content );
$field_content = str_replace( "_2_month'", "_2_month' autocomplete='cc-exp-month'", $field_content );
$field_content = str_replace( "_2_year'", "_2_year' autocomplete='cc-exp-year'", $field_content );
$field_content = str_replace( ".3' id", ".3' autocomplete='cc-csc' id", $field_content );
$field_content = str_replace( ".5' id", ".5' autocomplete='cc-name' id", $field_content );
}
return $field_content;
}, 10, 2 );
This example shows how you can edit the input markup to add the autocomplete=’off’ for the Address field Country drop down.
add_filter( 'gform_field_content', function ( $field_content, $field ) {
if ( $field->type == 'address' ) {
return str_replace( "name='input_3.6'", "autocomplete='off' name='input_3.6'", $field_content );
}
return $field_content;
}, 10, 2 );
See the following Google article for more detail on the autocomplete attribute.
3. Remove field from Entry Detail
This example shows how you can prevent a field (id 2) being displayed on the entry detail page.
add_filter( 'gform_field_content', function ( $field_content, $field, $value ) {
// Change 2 to the id number of your field.
if ( $field->id == 2 ) {
if ( $field->is_entry_detail_edit() ) { // Only for edit entry screen.
$value = esc_attr( $value );
$name = 'input_' . esc_attr( $field->id );
return "<input type='hidden' name='{$name}' value='{$value}'>";
} elseif ( $field->is_entry_detail() ) { // Only for entry details screen.
return '';
}
}
return $field_content;
}, 10, 3 );
4. Display Section field description on Entry Detail
add_filter( 'gform_field_content', function ( $field_content, $field ) {
if ( ! GFCommon::is_entry_detail_view() ) {
return $field_content;
}
if ( $field->type == 'section' ) {
$field_content .= sprintf( '<tr><td colspan="2" class="entry-view-field-value">%s</td></tr>', $field->description );
}
return $field_content;
}, 10, 2 );
5. Change a field to be readonly
add_filter( 'gform_field_content', function( $field_content, $field ) {
if ( $field->id == 1 || $field->id == 51 ) {
return str_replace( 'type=', "readonly='readonly' type=", $field_content );
}
return $field_content;
}, 10, 2 );
6. Add HTML tags to some text in your field
This example shows how to wrap a word in a field between HTML tags, an use case for this would be to add custom formatting to the text in your field label. In this case we’re also using the form id in the filter name to limit its action to a single form.
add_filter( 'gform_field_content_45', function( $field_content, $field ) {
if ( $field->id == 1 ) {
return str_replace( 'Best', "<em>Best</em>", $field_content );
}
return $field_content;
}, 10, 2 );
7. Add a custom CSS class
This example shows how to add a custom CSS class ( your_class ) to a field with id 17 on a form with id 758.
add_filter( 'gform_field_content', function ( $content, $field, $value, $lead_id, $form_id ) {
GFCommon::log_debug( __METHOD__ . '(): modified field content ' . $content );
// Change 758 and 17 to your form id and field id respectively.
if ( $form_id == 758 && $field->id == 17 ) {
return str_replace( "class='", "class='your_class ", $content );
}
return $content;
}, 10, 5 );
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in the following methods:
- GFEntryDetail::lead_detail_edit() in entry_detail.php
- GFEntryDetail::lead_detail_grid() in entry_detail.php
- GFFormDisplay::get_field_content() in form_display.php