Add Name Attribute to GravityForms Form Tag

Authored by

Here you will find the code that will add the name="" attribute to the <form> tag of all gravity forms that is based on the title of the form. Can be useful if you are wanting to turn on GA4 Form Interactions within the GA4 data stream.

add_filter( 'gform_form_tag', 'modify_gf_tag', 10, 2 );
function modify_gf_tag( $form_tag, $form ) {
    if ( empty( $form_tag ) ) {
        // Don't add the input when other integrations have removed the form tag.
        return $form_tag;
    }
    $title = rgar( $form, 'title' );
    // Sanitize the title to not contain any double quotes if it does replace it with a single quote.
    $title = str_replace('"', "'", $title);

    //check to see if name already exists in the form tag
    if (strpos($form_tag, "|name=|") === false ) 
        $form_tag = preg_replace( "|action='|", 'name="'.$title.'" action=\'', $form_tag );

    return $form_tag;
}

 

Other Code Snippets

Change Permissions or Ownership of Files or Directories Recursively

Code Snippet | Linux

Sometimes when you are moving files from one host to another, you have to change the files owner or permissions so the web server can utilize it properly.

Read More
Find Files by Size

Code Snippet | Linux

Here is a Linux command line that searches for files that are over a certain size so that you can find where you might be able to free up some space. Useful to find error_logs that have run amok.

Read More
Import MySQL from Command Line

Code Snippet | Linux | MySQL

Here is a method to import a SQL file into a database via the Linux command line.

Read More
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram