Gravity Forms - for your WordPress form needs

How to Make Fields Read Only with Gravity Forms

| 6 Comments

This quick tip shows you how to make fields in Gravity Forms read-only, or greyed out if you will.

This is useful for when you need to pull information into a Gravity Form (such as pulling user info into Gravity Forms, or using Restrict Content Pro access levels in Gravity Forms), but you don’t want the user to be able to edit it.

In a recent example, I wanted to show my clients at The WP Butler what their development rate discount was, which is based on their plan.

In order to do this, I created a field which showed the rate discount conditionally, based on their access level, but I wanted to keep the field read-only, so that they could only view that value.

To do so, I added the following snippet to my functionality plugin:

// DISABLE CERTAIN FIELDS

function enqueue_gf_disable() {
	wp_enqueue_script( 'gf-disable', plugins_url( '/scripts/gravity-forms-disable.js', dirname(__FILE__) ) );
}

add_action( 'wp_enqueue_scripts', 'enqueue_gf_disable' );

This was placed in a file which was inside a subdirectory of my functionality plugin. If the file was in the root directory of my functionality plugin, dirname(__FILE__) would have just become __FILE__.

All this does is load a JavaScript file from the location specified. In this example, it’s in a subdirectory of my functionality plugin called scripts, and the file is called gravity-forms-disable.js.

The file gravity-forms-disable.js contains the following simple code:

jQuery(document).ready(function($){
	$(".gform_wrapper .disable input").attr('disabled','disabled');
});

Greyed out Gravity Forms FieldAll this does is look for Gravity Forms fields with a CSS class of “disable” and makes them read only. So, now that your code is in place and properly enqueued, all you need to do is find the field you want to make read-only and apply a CSS class of “disable” to it, and the field will become read-only.

How to Make Gravity Forms Fields Read-Only

Quite an easy thing to put in place, and very simple to use. What applications have you used this in?

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Keep your site backed up, updated & secure

I provide a service called The WP Butler, which helps you stay on top of the maintenance of your WordPress site. Instead of worrying about whether your site is secure, updated and backed up, The WP Butler handles all that for you on a regular basis, so that you can focus on doing what you do best. If you use coupon DIWW, you'll save 15% on our already-low-prices for all maintenance plans.

Author: Dave Clements

Dave Clements has been building websites for close to a decade and in 2010, he formalised that by starting his own company, The UK Edge. He now works on a variety of web projects, from simple tasks like installing a new WordPress site, to consulting on problems, or redesigning his clients' sites. He also runs Do It With WordPress, a site dedicated to providing free tutorials on WordPress. When he's not building your new website, you can find Dave eating Wheat Thins, spending time with friends and family, watching Indie films, fostering kittens from the local Humane Society, listening to some dubstep, dance and electronic rock, and exploring the world.

6 Comments

  1. I love (and work for) Gravity Forms so I’m always stoked to see snippets like these! This is a good use-case for read-only functionality.

    I’ve actually got a perk over at Gravity Wiz that also provides read-only functionality. One thing you might like about the perk-version, is that it does not rely on any script to add the read-only attribute but rather updates the form’s markup before it is output to the screen.

    If anyone is interested in checking it out, head over to the Available Perks page, scroll down to the “GP Read Only” perk and use the “Help Test This Perk” link to drop me a line and get the beta.

    http://gravitywiz.com/available-perks/

    • Thanks for the link David. I’d been following along with your Perks initiative recently and had noted the Read Only functionality in there, which I had not long added to one of my own sites (hence the tutorial explaining it).

  2. Hi Dave,

    Thanks for this but I can’t seem to get this to work. It doesn’t seem too complicated.

    1- I installed/activated the plugin.

    2- Edited it to include the snippet with __FILE__ instead of dirname(__FILE__).

    3- I created the js file and uploaded it to /my-functionality-plugin/scripts/

    4- And I correctly added the “disable” class to the form field.

    Basically I followed your instructions to the letter. When I check the source of the form page I see that the “disable” class has been sent.

    Any thoughts?

    • Are you able to confirm from the page source whether the JS file is being correctly enqueued in the header? Can you confirm that you put the snippet in my-functionality-plugin/my-functionality-plugin.php?

  3. No…Turns out the JS file wasn’t being loaded so I included it in the plugin.

    The folder structure was like this:

    - my-functionality-plugin
    — my-functionality-plugin.php
    – scripts
    — gravity-forms-disable.js

    I like your way better. How can I confirm that the script is being enqueued correctly?

    Thanks for your help.

    • If you look at the page source, there’s a section in the head of the document where WordPress loads all of its styles (CSS) and scripts. Your .js file should be loaded in that section. If it’s being loaded correctly (and loading the correct file), then it’s something in the JS. If it’s not, then it’s in your PHP, causing the file not to enqueue properly.

Leave a Reply