Advertisement
salmancreation

placeholder in gravity form

Jun 22nd, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. Put this code in your functions.php
  2.  
  3. <?php
  4. /* Add a custom field to the field editor (See editor screenshot) */
  5. add_action("gform_field_standard_settings", "my_standard_settings", 10, 2);
  6.  
  7. function my_standard_settings($position, $form_id){
  8.  
  9. // Create settings on position 25 (right after Field Label)
  10.  
  11. if($position == 25){
  12. ?>
  13.        
  14. <li class="admin_label_setting field_setting" style="display: list-item; ">
  15. <label for="field_placeholder">Placeholder Text
  16.  
  17. <!-- Tooltip to help users understand what this field does -->
  18. <a href="javascript:void(0);" class="tooltip tooltip_form_field_placeholder" tooltip="&lt;h6&gt;Placeholder&lt;/h6&gt;Enter the placeholder/default text for this field.">(?)</a>
  19.            
  20. </label>
  21.        
  22. <input type="text" id="field_placeholder" class="fieldwidth-3" size="35" onkeyup="SetFieldProperty('placeholder', this.value);">
  23.        
  24. </li>
  25. <?php
  26. }
  27. }
  28.  
  29. /* Now we execute some javascript technicalitites for the field to load correctly */
  30.  
  31. add_action("gform_editor_js", "my_gform_editor_js");
  32.  
  33. function my_gform_editor_js(){
  34. ?>
  35. <script>
  36. //binding to the load field settings event to initialize the checkbox
  37. jQuery(document).bind("gform_load_field_settings", function(event, field, form){
  38. jQuery("#field_placeholder").val(field["placeholder"]);
  39. });
  40. </script>
  41.  
  42. <?php
  43. }
  44.  
  45. /* We use jQuery to read the placeholder value and inject it to its field */
  46.  
  47. add_action('gform_enqueue_scripts',"my_gform_enqueue_scripts", 10, 2);
  48.  
  49. function my_gform_enqueue_scripts($form, $is_ajax=false){
  50. ?>
  51. <script>
  52.  
  53. jQuery(function(){
  54. <?php
  55.  
  56. /* Go through each one of the form fields */
  57.  
  58. foreach($form['fields'] as $i=>$field){
  59.  
  60. /* Check if the field has an assigned placeholder */
  61.            
  62. if(isset($field['placeholder']) && !empty($field['placeholder'])){
  63.                
  64. /* If a placeholder text exists, inject it as a new property to the field using jQuery */
  65.                
  66. ?>
  67.                
  68. jQuery('#input_<?php echo $form['id']?>_<?php echo $field['id']?>').attr('placeholder','<?php echo $field['placeholder']?>');
  69.                
  70. <?php
  71. }
  72. }
  73. ?>
  74. });
  75. </script>
  76. <?php
  77. }
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement