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');
});
All 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.

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


January 28, 2013 at 2:03 pm
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/
January 28, 2013 at 4:17 pm
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).
February 28, 2013 at 3:08 pm
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?
March 1, 2013 at 8:54 am
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?
March 1, 2013 at 9:07 am
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.
March 1, 2013 at 2:24 pm
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.