</script>We're using 20
as the priority to add_action()
to make sure the Gravity Forms JavaScript code is loaded along with all its dependencies, including jQuery, which we'll use to listen for the gform_post_render
event. Since Gravity Forms uses the default priority of 10
, we need to use a higher priority.
So let's listen for the gform_post_render
event.
<?php
/**
* Plugin Name: Example Custom Code
*/
function example_wp_head() {
?>
<script>
jQuery( document ).on(
'gform_post_render',
function() {
}
);
</script>
<?php
}
add_action( 'wp_head', 'example_wp_head', 20 );
Be aware that this is a jQuery event, not a Gravity Forms hook, so we need to use the jQuery on()
method to listen for it, not the Gravity Forms gform.addAction()
or gform.addFilter()
.
Now that we can execute code as soon as a Gravity Form is rendered, we can simply look for the input we want and disable it.
Gravity Forms inputs always have an ID that starts with input_
and is followed by the form ID, an underscore, and the field ID. So, for example, if we wanted to target field 10 from form 1, we'd need to look for an element with the ID input_1_10
. And since we have jQuery available, we can use
to find it.jQuery( '#input_1_10' )
Once found, it's a simple matter of making it read only it by adding the readonly
attribute. We can do this with the jQuery attr()
method that's available on all jQuery objects.
<?php
/**
* Plugin Name: Example Custom Code
*/
function example_wp_head() {
?>
<script>
jQuery( document ).on(
'gform_post_render',
function() {
jQuery( '#input_1_10' ).attr(
'readonly', true
);
}
);
</script>
<?php
}
add_action( 'wp_head', 'example_wp_head', 20 );
And that's it! We've made the date input read only.
Now users can only select a date from the date picker, and typing a date in the input is no longer possible.
With Hero for Gravity Forms, it's even easier to make a Gravity Forms date input read only. Simply check “Read Only Input” in the Appearance section!