Skip to content

Instantly share code, notes, and snippets.

@westcoastdigital
Created June 8, 2019 03:49
Show Gist options
  • Save westcoastdigital/688570d804ddfae092ec73fb4e6a64b4 to your computer and use it in GitHub Desktop.
Save westcoastdigital/688570d804ddfae092ec73fb4e6a64b4 to your computer and use it in GitHub Desktop.
Enable option to make GF fields readonly
<?php
add_filter('gform_field_content', function ($field_content, $field) {
$fieldtype = $field['type'];
if( $fieldtype == 'text' || $fieldtype == 'email' || $fieldtype == 'number' || $fieldtype == 'phone' ) {
$readonlySupport = 'true';
} else {
$readonlySupport = 'false';
}
$visibility = $field['visibility'];
if ($visibility == 'readonly' && $readonlySupport == 'true' ) {
$field_content = str_replace('type=', "readonly='readonly' type=", $field_content);
}
elseif ($visibility == 'readonly' && $field['type'] == 'textarea' ) {
$field_content = str_replace('name=', "readonly name=", $field_content);
}
elseif ($visibility == 'readonly' && $field['type'] == 'checkbox' ) {
$field_content = str_replace('type=', "disabled='disabled' type=", $field_content);
}
elseif ($visibility == 'readonly' && $field['type'] == 'radio' ) {
$field_content = str_replace('type=', "disabled='disabled' type=", $field_content);
}
return $field_content;
}, 10, 2);
add_filter( 'gform_visibility_options', 'wcd_gf_readonly_visibility_options' );
function wcd_gf_readonly_visibility_options( $options ) {
$options[] = array(
'label' => __( 'Read Only', 'wcd' ),
'value' => 'readonly',
'description' => __( 'Set the field to read only so value cannot be edited.', 'wcd' )
);
return $options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment