Using Gravity Forms ‘gform_form_tag’ PHP action

The gform_form_tag filter allows you to modify the form tag (i.e. <form method="post">) when a Gravity Forms form is displayed.

Usage

A generic example to apply the filter to all forms:

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

To apply the filter to a specific form, append the form ID to the end of the hook name (format: gform_form_tag_FORMID):

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

Parameters

  • $form_tag (string) – The string containing the <form> tag.
  • $form (Form Object) – The current form.

More information

See Gravity Forms Docs: gform_form_tag

Examples

Submit Form to Custom Handler

This example changes the action of the form tag, submitting the form to a custom form handler.

add_filter('gform_form_tag', 'form_tag', 10, 2);
function form_tag($form_tag, $form) {
    if ($form['id'] != 3) {
        // Not the form whose tag you want to change, return the unchanged tag.
        return $form_tag;
    }
    $form_tag = preg_replace("|action='(.*?)'|", "action='custom_handler.php'", $form_tag);
    return $form_tag;
}

Turn off Autocompletion

This example turns off autocompletion for a form with ID 1. To apply it to all forms, simply remove the if statement.

add_filter('gform_form_tag', 'form_tag', 10, 2);
function form_tag($form_tag, $form) {
    if ($form['id'] != 1) {
        // Not the form whose tag you want to change, return the unchanged tag.
        return $form_tag;
    }
    // Turn off autocompletion as described here https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
    $form_tag = preg_replace("|action='|", "autocomplete='off' action='", $form_tag);
    return $form_tag;
}

Add Custom CSS Class for Save and Continue Forms

This example adds a custom CSS class to forms when they’re accessed using a Save and Continue link. This assumes that your form CSS Class Name setting is empty.

add_filter('gform_form_tag', function ($form_tag, $form) {
    $gf_token = rgget('gf_token');
    if (!empty($gf_token)) {
        // Add class only if value for gf_token is provided.
        $form_tag = preg_replace("|action='|", "class='my_super_class' action='", $form_tag);
    }
    return $form_tag;
}, 10, 2);

Add Hidden Input

This example shows how you can add a custom input to the form without including a field in the form fields array.

add_filter('gform_form_tag', function ($form_tag, $form) {
    if (empty($form_tag)) {
        // Don't add the input when other integrations have removed the form tag e.g. Gravity Flow workflow detail page.
        return $form_tag;
    }
    $name = wp_hash(GFForms::$version . rgar($form, 'id') . 'your_custom_key');
    $class = 'your_custom_class';

$form_tag .= sprintf("", esc_attr($name), esc_attr($class));
return $form_tag;
}, 60, 2);

Change Form Method to GET

This example changes the form method from POST to GET for a form with ID 4.

add_filter('gform_form_tag', 'change_form_method', 10, 2);
function change_form_method($form_tag, $form) {
    if ($form['id'] != 4) {
        // Not the form whose tag you want to change, return the unchanged tag.
        return $form_tag;
    }
    $form_tag = preg_replace("|method='post'|", "method='get'", $form_tag);
    return $form_tag;
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFFormDisplay::get_form() in form_display.php.

Third-party Resources

  • Plugin: Gravity Forms Tag Editor

A simple plugin that makes modifying Gravity Forms tags a breeze. Change any attribute of the form tag with just a few lines of code. Visit the plugin page.