-
Notifications
You must be signed in to change notification settings - Fork 0
/
placehold-gravity-forms.php
85 lines (77 loc) · 3.42 KB
/
placehold-gravity-forms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Plugin Name: Placehold Gravity Forms
* Plugin URI: https://github.com/51seven/placehold-gravity-forms
* Description: Adds a placeholder to inputs Gravity Forms.
* Version: 1.2
* Author: 51seven GmbH, Timo Maemecke
* Author URI: http://51seven.de
* License: MIT
*/
/**
* Adds the placeholder input to the field of Gravity Forms.
* HTML Code is the same as in every input of a Gravity field,
* it get's echo'ed on position 25 (right after the name of the field).
*
* Attribution goes to http://www.wpbeginner.com/wp-tutorials/how-to-add-placeholder-text-in-gravity-forms/
*
* Hook: gform_field_standard_settings
*/
function placehold_gform_field_standard_settings($position, $form_id) {
if($position == 25) {
$output = '<li class="admin_label_setting field_setting" style="display: list-item; ">';
$output .= '<label for="field_placeholder">Placeholder'
.' <a href="#" onclick="return false;" class="gf_tooltip tooltip tooltip_form_field_label" title="<h6>Placeholder</h6>This text will be displayed as the HTML5 Placeholder of the input field."><i class="fa fa-question-circle"></i></a>'
.' <a href="#" onclick="return false;" class="gf_tooltip tooltip tooltip_form_field_label_html" title="<h6>Placeholder</h6>This text will be displayed as the HTML5 Placeholder of the input field." style="display: none;"><i class="fa fa-question-circle"></i></a>'
.'</label>';
$output .= '<input type="text" id="field_placeholder" class="fieldwidth-3" size="35" onkeyup="SetFieldProperty(\'placeholder\', this.value);">';
$output .= '</li>';
echo $output;
}
}
/**
* Sets the saved content of the placeholder to the value of the field with jQuery.
*
* Attribution goes to http://www.wpbeginner.com/wp-tutorials/how-to-add-placeholder-text-in-gravity-forms/
*
* Hook: gform_editor_js
*/
function placehold_gform_editor_js() {
$output = '<script>'
.'jQuery(document).bind("gform_load_field_settings", function(event, field, form) {'
.'jQuery("#field_placeholder").val(field["placeholder"]);'
.'});'
.'</script>';
echo $output;
}
/**
* Adds the placeholder attribute with the saved content to the input.
* This will happen without jQuery, due the fact that all JavaScript related
* things should be included right before the ending body tag, not in the
* middle of a document.
* You don't need to include jQuery in your head. You don't need to add jQuery anywhere.
*
* It parses the HTML with a DOMDocument Object and adds the placeholder to every input element
*/
function placehold_gform_field_content($content, $field, $value, $lead_id, $form_id) {
if(isset($field['placeholder']) && !empty($field['placeholder'])) {
$dom = new DOMDocument();
$utf8content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
@$dom->loadHTML($utf8content);
$path = new DOMXPath($dom);
foreach($path->query("//input") as $node) {
$node->setAttribute("placeholder", $field['placeholder']);
}
foreach($path->query("//textarea") as $node) {
$node->setAttribute("placeholder", $field['placeholder']);
}
$content = preg_replace(array("/^\<\!DOCTYPE.*?<html><body>/si", "!</body></html>$!si"), "", $dom->saveHtml());
}
return $content;
}
/**
* Calling all Hooks
*/
add_action("gform_field_standard_settings", "placehold_gform_field_standard_settings", 10, 2);
add_action("gform_editor_js", "placehold_gform_editor_js");
add_filter("gform_field_content", "placehold_gform_field_content", 10, 5);