Using Gravity Forms ‘gform_postimage_file’ PHP action

The gform_postimage_file is a filter in Gravity Forms that is used when creating the Post Image File field. It allows you to modify the “File” label for the Post Image File field.

Usage

This filter applies to all forms. Here is a general use case:

add_filter( 'gform_postimage_file', 'your_function_name', 10, 2 );

If you want to target a specific form, you append the form id to the hook name like so:

add_filter( 'gform_postimage_file_5', 'your_function_name', 10, 2 );

In the above examples, replace your_function_name with the name of your function where you’ll have your custom code.

Parameters

  • $label (string): The label to be filtered.
  • $form_id (integer): The current form’s id.

More information

For more details, you can check out the Gravity Forms Docs: gform_postimage_file

This filter is located in GF_Field_Post_Image::get_field_input() in includes/fields/class-gf-field-post-image.php.

Examples

Changing Default Post Image File Label

This example changes the default Post Image File label to ‘Image File’.

add_filter( 'gform_postimage_file', 'change_postimage_file', 10, 2 );
function change_postimage_file( $label, $form_id ) {
    return 'Image File';
}

In this code, the function change_postimage_file is created which accepts two parameters, $label and $form_id. It simply returns ‘Image File’, thereby changing the default label.

Change Label for Specific Form

This example changes the Post Image File label to ‘Custom Label’ for the form with the id of 5.

add_filter( 'gform_postimage_file_5', 'change_label_for_form_5', 10, 2 );
function change_label_for_form_5( $label, $form_id ) {
    return 'Custom Label';
}

Here, we are targeting a specific form with the id of 5. The function change_label_for_form_5 changes the label to ‘Custom Label’ for this specific form.

Conditional Label Change

This example changes the Post Image File label based on form id.

add_filter( 'gform_postimage_file', 'conditional_label_change', 10, 2 );
function conditional_label_change( $label, $form_id ) {
    if ($form_id == 5) {
        return 'Label for Form 5';
    }
    return 'Default Label';
}

In this example, the function conditional_label_change checks the form id and changes the label to ‘Label for Form 5’ if the form id is 5. For all other forms, it returns ‘Default Label’.

Different Labels for Multiple Forms

This example shows how you can set different labels for different forms.

add_filter( 'gform_postimage_file', 'multiple_form_labels', 10, 2 );
function multiple_form_labels( $label, $form_id ) {
    $form_labels = array(
        5 => 'Label for Form 5',
        7 => 'Label for Form 7',
        10 => 'Label for Form 10'
    );
    return array_key_exists($form_id, $form_labels) ? $form_labels[$form_id] : $label;
}

Here, we have an array form_labels that contains the custom labels for forms with ids 5, 7, and 10. The function multiple_form_labels checks if the current form’s id exists in the form_labels array. If it does, it returns the corresponding label. If it doesn’t, it returns the default label.

Reset Label to Default

This example resets the Post Image File label to its default value for all forms.

add_filter( 'gform_postimage_file', 'reset_label_to_default', 10, 2 );
function reset_label_to_default( $label, $form_id ) {
    return 'File';
}

In this case, no matter what the current label is, the function reset_label_to_default will always return ‘File’, which is the default label.