So You Want to Change the Gravity Forms Form Tag…

I had an interesting request from a client today. They use Pardot to integrate with the forms on their old website, and wanted to do the same with their new WordPress theme. We’re using the always spectacular Gravity Forms for the forms on the site.

I did the integrations, and we got an error.

The form enc type appears to be incompatible with our pardot form handler according to this article:
 
Wrong Enctype
Pardot form handlers can only be integrated with forms using an empty enctype attribute or an enctype of application/x-www-form-urlencoded. Pardot doesn't accept an enctype of multipart/form-data.

I didn’t realize (but now it makes perfect sense) that Gravity Forms defaults to enctype=”multipart/form-data”. I scoured the Gravity Forms settings, looking for a way to switch that to the correct enctype without any luck. I hit up a good friend of mine, Rob Harrell, on Twitter, asking if there was a way to edit the form. Thankfully, there is. He sent me this page about a filter a developer can use the edit the form tag output by Gravity Forms.

I added this code to the functions.php file to change the enctype of the form:

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;
    }
    $form_tag = preg_replace( "|enctype='(.*?)'|", "enctype='application/x-www-form-urlencoded'", $form_tag );
    return $form_tag;
}

Breaking down that code:

//Enter the ID of the form you want to alter here:
if ( $form['id'] != 1 ) {

//Alter the 
tag however you like here: $form_tag = preg_replace( "|enctype='(.*?)'|", "enctype='application/x-www-form-urlencoded'", $form_tag ); //This can be just about any replacement you want, or you can just return a new string that is the new form tag return '

Pardot is picky about the form they integrate with, and thankfully the Gravity Forms developers made it possible to alter the <form> tag however we need to.