Advertisement
shakil97bd

How to Add Placeholder Text in Gravity Forms

Oct 17th, 2014
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. *****************How to Add Placeholder Text in Gravity Forms******************
  2.  
  3. If you don’t know already, Gravity Forms by far is the most beginner friendly WordPress contact form plugin out there. We use it on our WordPress Gallery site, WordPress Coupons site, and pretty much all new clients as well. Recently while working on a client’s site we had to tweak the styling of the form to match the design which required us to put placeholder text in Gravity Forms. Unfortunately and surprisingly, this feature is not built-in to Gravity Forms (Yet). While they do have an option to add placeholder text in drop down fields and post field: category, there is no option to add placeholder on input fields and textarea fields. This became a huge issue for us. We searched through the Gravity forms support area where the only solution available was a hack which didn’t even work properly. While it allowed us to put placeholder text, it had major issues. If the user just hit submit without actually filling the field, the form will validate rather than returning errors. After a good search, we ended up finding the solution. In this article, we will show you how to add placeholder text in gravity forms using jQuery and Gravity Form filters.
  4.  
  5. So you are probably wondering why heck did we need placeholder text when labels are there? Well in the design we were working on, we couldn’t use labels for styling purposes.
  6.  
  7. ************************************%%%%%%%%%%%%%%%%%%%%%*********************************************
  8.  
  9. Final Code
  10.  
  11. The final code is below. You can simply copy and paste this in your functions.php file and have it working. But if you want to read more about the function and how it works, then continue reading the article. Also continue reading to see how to add placeholder text on dropdown fields.
  12.  
  13. <!---- Final Code ----->
  14.  
  15. <?php
  16. /* Add a custom field to the field editor (See editor screenshot) */
  17. add_action("gform_field_standard_settings", "my_standard_settings", 10, 2);
  18.  
  19. function my_standard_settings($position, $form_id){
  20.  
  21. // Create settings on position 25 (right after Field Label)
  22.  
  23. if($position == 25){
  24. ?>
  25.        
  26. <li class="admin_label_setting field_setting" style="display: list-item; ">
  27. <label for="field_placeholder">Placeholder Text
  28.  
  29. <!-- Tooltip to help users understand what this field does -->
  30. <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>
  31.            
  32. </label>
  33.        
  34. <input type="text" id="field_placeholder" class="fieldwidth-3" size="35" onkeyup="SetFieldProperty('placeholder', this.value);">
  35.        
  36. </li>
  37. <?php
  38. }
  39. }
  40.  
  41. /* Now we execute some javascript technicalitites for the field to load correctly */
  42.  
  43. add_action("gform_editor_js", "my_gform_editor_js");
  44.  
  45. function my_gform_editor_js(){
  46. ?>
  47. <script>
  48. //binding to the load field settings event to initialize the checkbox
  49. jQuery(document).bind("gform_load_field_settings", function(event, field, form){
  50. jQuery("#field_placeholder").val(field["placeholder"]);
  51. });
  52. </script>
  53.  
  54. <?php
  55. }
  56.  
  57. /* We use jQuery to read the placeholder value and inject it to its field */
  58.  
  59. add_action('gform_enqueue_scripts',"my_gform_enqueue_scripts", 10, 2);
  60.  
  61. function my_gform_enqueue_scripts($form, $is_ajax=false){
  62. ?>
  63. <script>
  64.  
  65. jQuery(function(){
  66. <?php
  67.  
  68. /* Go through each one of the form fields */
  69.  
  70. foreach($form['fields'] as $i=>$field){
  71.  
  72. /* Check if the field has an assigned placeholder */
  73.            
  74. if(isset($field['placeholder']) && !empty($field['placeholder'])){
  75.                
  76. /* If a placeholder text exists, inject it as a new property to the field using jQuery */
  77.                
  78. ?>
  79.                
  80. jQuery('#input_<?php echo $form['id']?>_<?php echo $field['id']?>').attr('placeholder','<?php echo $field['placeholder']?>');
  81.                
  82. <?php
  83. }
  84. }
  85. ?>
  86. });
  87. </script>
  88. <?php
  89. }
  90. ?>
  91.  
  92.  
  93.  
  94. <!---- End Final Code ----->
  95.  
  96.  
  97. Note: Please delete the first php simbul and last ending of php after past this code in fu nctions.php of your theme.
  98.  
  99. Referral link:- http://www.wpbeginner.com/wp-tutorials/how-to-add-placeholder-text-in-gravity-forms/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement