Skip to content

Instantly share code, notes, and snippets.

@jimi008
Last active June 6, 2016 12:31
Show Gist options
  • Save jimi008/2db3fd2e498df1488399583d981c2c73 to your computer and use it in GitHub Desktop.
Save jimi008/2db3fd2e498df1488399583d981c2c73 to your computer and use it in GitHub Desktop.
GF html5 placeholder
<?php
add_action("gform_field_standard_settings", "jtd_gform_placeholders", 10, 2);
function jtd_gform_placeholders($position, $form_id){
// Create settings on position 25 (right after Field Label)
if ( $position == 25 ) {
?>
<li class="admin_label_setting field_setting" style="display: list-item; ">
<label for="field_placeholder">Placeholder Text
<!-- Tooltip to help users understand what this field does -->
<a href="javascript:void(0);" class="tooltip tooltip_form_field_placeholder" tooltip="<h6>Placeholder</h6>Enter the placeholder/default text for this field.">(?)</a>
</label>
<input type="text" id="field_placeholder" class="fieldwidth-3" size="35" onkeyup="SetFieldProperty('placeholder', this.value);">
</li>
<?php
}
}
/* Now we execute some javascript technicalities for the field to load correctly */
add_action("gform_editor_js", "jtd_gform_editor_js");
function jtd_gform_editor_js(){
?>
<script>
//binding to the load field settings event to initialize the checkbox
jQuery(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#field_placeholder").val(field["placeholder"]);
});
</script>
<?php
}
/* We use jQuery to read the placeholder value and inject it to its field */
add_action('gform_pre_render',"jtd_gform_init_scripts", 10, 2);
function jtd_gform_init_scripts( $form ) {
$script = "";
/* Go through each one of the form fields */
foreach($form['fields'] as $i=>$field) {
/* Check if the field has an assigned placeholder */
if(isset($field['placeholder']) && !empty($field['placeholder'])){
/* If a placeholder text exists, inject it as a new property to the field using jQuery */
$script .= "jQuery('#input_{$form['id']}_{$field['id']}').attr('placeholder','{$field['placeholder']}');";
}
}
GFFormDisplay::add_init_script($form["id"], 'jtd_placeholder', GFFormDisplay::ON_PAGE_RENDER, $script);
return $form;
}
?>
jQuery(document).ready(function($){
$('#input_5_1').attr('placeholder','Name');
});
<script>
jQuery(document).ready(function($){
$('#input_5_1').attr('placeholder','Name');
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment