gform_form_tag

Description

This filter is executed when the form is displayed and can be used to completely change the form tag (i.e. <form method=”post”>).

Usage

The following would apply to all forms.

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

To limit the scope of your function 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.

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

Turn off autocompletion as described in Mozilla developer docs: How to Turn Off Form Autocompletion

The following will be applied only to a form with id 1.

add_filter( 'gform_form_tag', 'form_tag', 10, 2 );
function form_tag( $form_tag, $form ) {
    if ( $form['id'] != 1 ) { // change 1 to match your form id
        // 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;
}

If you want 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 ) {
    // 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

The following example shows you how to add a custom CSS class to forms when they’re accessed using a Save and Continue link. This example 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( "<input type='hidden' name='%s' class='%s' value='' />", esc_attr( $name ), esc_attr( $class ) );

	return $form_tag;
}, 60, 2 );

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.