Skip to content

Instantly share code, notes, and snippets.

@remcotolsma
Last active August 29, 2015 13:58
Show Gist options
  • Save remcotolsma/10357847 to your computer and use it in GitHub Desktop.
Save remcotolsma/10357847 to your computer and use it in GitHub Desktop.
Gravity Forms readonly fields (no JavaScript).
<?php
/**
* Read only
*
* @param string $field_content
* @param array $field
* @return string
*/
function prefix_gform_field_readonly( $field_content, $field ) {
if ( isset( $field['cssClass'] ) && false !== strpos( $field['cssClass'], 'readonly' ) ) {
$attr = 'readonly="readonly"';
if ( false === strpos( $field_content, $attr ) ) {
$search = array(
'<input ',
'<textarea ',
'<select ',
);
$replace = array(
"<input $attr ",
"<textarea $attr ",
"<select $attr ",
);
$field_content = str_replace( $search, $replace, $field_content );
}
}
return $field_content;
}
add_filter( 'gform_field_content', 'prefix_gform_field_readonly', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment