Using Gravity Forms ‘gform_name_prefix’ PHP filter

The gform_name_prefix Gravity Forms PHP filter allows you to modify the “Prefix” label when creating the name prefix field.

Usage

A generic example of using the filter:

add_filter('gform_name_prefix', 'change_prefix', 10, 2);

To apply the filter to a specific form, use the form id. For example, form id 5:

add_filter('gform_name_prefix_5', 'change_prefix', 10, 2);

Parameters

  • $label (string): The label to be filtered.
  • $form_id (integer): The ID of the current form.

More information

See Gravity Forms Docs: gform_name_prefix

Examples

Change the default name prefix label

This example changes the default name prefix label to “Name Prefix”:

add_filter('gform_name_prefix', 'change_name_prefix', 10, 2);

function change_name_prefix($label, $form_id) {
    return "Name Prefix";
}

Change the name prefix label for a specific form

This example changes the name prefix label to “Title” for form ID 5:

add_filter('gform_name_prefix_5', 'change_title_prefix', 10, 2);

function change_title_prefix($label, $form_id) {
    return "Title";
}

Change the name prefix label based on form ID

This example changes the name prefix label based on the form ID:

add_filter('gform_name_prefix', 'change_prefix_based_on_form_id', 10, 2);

function change_prefix_based_on_form_id($label, $form_id) {
    if ($form_id == 5) {
        return "Title";
    } else {
        return "Prefix";
    }
}

Change the name prefix label and add custom text

This example changes the name prefix label to “Name Prefix (Optional)” for all forms:

add_filter('gform_name_prefix', 'change_name_prefix_optional', 10, 2);

function change_name_prefix_optional($label, $form_id) {
    return "Name Prefix (Optional)";
}

Modify the name prefix label using a conditional

This example changes the name prefix label to “Name Prefix (Mr./Ms.)” if the form ID is 5, and keeps the default label for other forms:

add_filter('gform_name_prefix', 'conditional_change_name_prefix', 10, 2);

function conditional_change_name_prefix($label, $form_id) {
    if ($form_id == 5) {
        return "Name Prefix (Mr./Ms.)";
    }
    return $label;
}