How to add custom HTML attributes to your Gravity Forms - Webfor (2024)

How to add custom HTML attributes to your Gravity Forms - Webfor (1)

Posted on August 30, 2019 by Webfor

Don’t want to read my life’s story and go straight to the tutorial? Click here.

Thanks to its ease of use and robust features, Gravity Forms is one of the most popular form builder plugins for WordPress. We use Gravity Forms for quite a few of our clients, and our own website as well, and we have been consistently happy with the results.

Sometimes, however, you may want greater control over your Gravity Forms’ HTML output. In our case, we’ve been conducting experiments hosting our website on Netlify, a static web app hosting service. Because Netlify sites are static (i.e., not running on a server), our existing Gravity Forms need some configuration to be able to work with Netlify’s built-in form handling, instead of Gravity Form’s default PHP processing through WordPress.

According to Netlify’s documentation, our <form>s need to have the attribute data-netlify="true" in order for Netlify’s built-in robots to recognize and process the forms, and a name attribute in order for Netlify to identify the form in their backend.

Without further ado, here is the code we added to our WordPress site’s functions.php to get this to work.

//Add Netflify tag to GFormsadd_filter( 'gform_form_tag', 'form_tag_netlify', 10, 2 );function form_tag_netlify( $form_tag, $form ) { $form_name = $form['title']; $form_tag = str_replace( "<form ", "<form name='{$form_name}' data-netlify='true' ", $form_tag ); return $form_tag;}

One of my biggest pet peeves with some WordPress tutorials online is that they don’t explain why the code examples work, so let’s go ahead and break this down.

  • First, we are calling the gform_form_tag filter. This is a filter that comes built-in with Gravity Forms, and allows you to alter what each form outputs.
  • form_tag_netlify is the name of the function we defined ourselves. In your case, you would change this to a name that more closely matches what you are trying to accomplish with your function.
  • Next, we have our filter’s priority set to 10, i.e., the relative priority our function has against other functions using the gform_form_tag filter. In most cases, we can leave this at the default 10, but if you run into issues, you could try increasing the priority to a higher number.
  • 2 is saying how many parameters to expect from our function. The reason this is set to “2” is because our form_tag_netlify function takes two parameters, $form and $form_tag. If the numbers don’t match up, we’ll get an error.

Now to the function itself:

  1. function form_tag_netlify( $form_tag, $form ) is where we define the name of our function. Notice that the function name here is the same that we are passing through the filter we set previously. $form_tag and $form are each Gravity Form’s <form> HTML output and form object (with all of its associated data), respectively. These two variables are what we use to grab and manipulate our form’s attributes.
  2. Netlify forms need a name attribute on the form in the front-end, so let’s go ahead and define a name for our form with this line of code:
$form_name = $form["title"];

This is grabbing the Gravity Form’s “title” attribute (which is the name you set for your form when creating it in the WordPress backend) from the $form object. In our case, this title is “Get Started”. We will use this when rebuilding our <form> element later.

  1. Next is to update the <form> output itself. This is where $form_tag comes in. We will take advantage of PHP’s str_replace() function to add more attributes to our form:
$form_tag = str_replace( "<form ", "<form name='{$form_name}' data-netlify='true' ", $form_tag ); return $form_tag;

The first parameter is the string we’re looking to replace–in this case, <"form " (make sure to leave a trailing space so you don’t accidentally remove any other form attributes!).
The next parameter is what we are replacing our string with. Notice we are including the $form_name variable we defined earlier for our name attribute, and then adding data-netlify='true' (again, make sure you keep that trailing space).
The third parameter is telling str_replace() where to look. Since we only want our function to affect a Gravity Form’s HTML, we are only targeting the $form_tag.

  1. Finally, we return the updated value with:
return $form_tag;

Altogether, here is what the code you add to your functions.php file might look like:

//Add Netflify tag to GFormsadd_filter( 'gform_form_tag', 'form_tag_netlify', 10, 2 );function form_tag_netlify( $form_tag, $form ) { $form_name = $form['title']; $form_tag = str_replace( "<form ", "<form name='{$form_name}' data-netlify='true' ", $form_tag ); return $form_tag;}

Armed with this knowledge, you can adjust your Gravity Forms’ <form> tag output to become whatever you want.

Happy coding!

Questions, comments, found a way to make the code more efficient? Let us know in the comments below!

How to add custom HTML attributes to your Gravity Forms - Webfor (2024)

FAQs

How do I add HTML to Gravity Form? ›

Other Info
  1. Open your opt-in form page.
  2. Hover the form.
  3. Right-click to open the contextual menu.
  4. Click on "Inspect" the open the dev tool.
  5. Position at the form action line.
  6. Right-click and "Edit as HTML".
  7. Copy the form code.

How do you add attributes in Gravity Forms? ›

Under Form Settings -> Advanced, you can add your own attributes.

How do you add custom attributes in HTML? ›

If you want to define your own custom attributes in HTML, you can implement them through the data-* format. * can be replaced by any of your names to specify specific data to an element and target it in CSS, JavaScript, or jQuery.

What is the HTML field in Gravity Forms? ›

The HTML field allows you to place a block of free form HTML anywhere in your form. This is useful for adding images or additional content. It is available under the Standard Fields section within the form editor.

How do I add a custom script to Gravity Forms? ›

To add custom JavaScript, got to the Settings menu for your desired form and select the “Code Chest” item. Now just copy-and-paste your desired snippet into the JavaScript setting or start writing something from scratch, you little code pirate!

How do I add custom CSS to Gravity Form? ›

To add a Ready Class to a field, go to the Form Editor and select the field you want to change. Under the “Appearance” tab in the Settings Sidebar, you will see an input called “Custom CSS Class”. Add the Ready Class name or names you want to add to the field here and then save the form.

Can you embed Gravity Forms? ›

To embed your first Gravity Form, open the page or post you wish to add it to. Then, click the plus sign (+) to add a new block. In the Search for a block field, type “Gravity”. Under Embeds, click on Form.

Is there a character limit in Gravity Forms? ›

There is no character limit in Gravity Forms. Are you experiencing a character limit? If so you are experiencing a theme or plugin conflict.

Can I use custom HTML attributes? ›

You can also add custom HTML attributes from the Agent Settings tab if you prefer. Using the example, you can start collecting the data-id attribute by tracking everything beginning with data- and including the wildcard ( * ) rule: data-* . This means that every attribute that starts with data- will be recorded.

How do I add custom attributes dynamically in HTML? ›

Components
  1. Edit the main component that contains the element to which you want to add a class attribute.
  2. Select the element to which you want to add a class attribute.
  3. Go to Element settings panel > Custom attributes.
  4. Click the “plus” icon.
  5. Type class in the Name field.
  6. Click the purple “dot” icon in the Value field.

How do I import an attribute? ›

There are four steps to import Attributes:
  1. Create or Isolate the Required Columns.
  2. Create the Attribute Type Columns.
  3. Populate the Attribute Values.
  4. Assign the Attribute Types to Categories.
Mar 11, 2024

Can you convert HTML form to dynamic form using gravity form? ›

Yes, you can use the Pre-render hook to dynamically change the HTML block content. Here is a code snippet to point you in the right direction. Place it in your theme's function.

What is custom post type field in gravity form? ›

The Post Custom Field allows users to submit data that is then used to populate a custom field value for a post. This field works in tandem with the other Post fields to allow you to create a form that can generate post data from a user submission. It is available under the Post Fields section within the form editor.

How to add HTML in object? ›

The <object> tag defines a container for an external resource. The external resource can be a web page, a picture, a media player, or a plug-in application. To embed a picture, it is better to use the <img> tag. To embed HTML, it is better to use the <iframe> tag.

How do I display a form in HTML? ›

Example:
  1. <form>
  2. <label for="name">Enter name</label><br>
  3. <input type="text" id="name" name="name"><br>
  4. <label for="pass">Enter Password</label><br>
  5. <input type="Password" id="pass" name="pass"><br>
  6. <input type="submit" value="submit">
  7. </form>

Can you convert HTML form to dynamic form using Gravity Form? ›

Yes, you can use the Pre-render hook to dynamically change the HTML block content. Here is a code snippet to point you in the right direction. Place it in your theme's function.

How do I create a dynamic form in HTML? ›

To create an HTML form, you need to use the <form> tag and specify the action and method attributes. The action attribute defines the URL of the server script that handles the form data, and the method attribute defines the HTTP method to use, such as GET or POST.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5367

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.