ForbodingAngel

Untitled

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