Advertisement
Eckstein

sdForms

Jul 19th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name:Smart Deploy Forms
  4. Description: Custom forms for SmartDeploy website
  5. */
  6.  
  7. function sdForm($atts) {
  8. //Check for a form id variable in the shortcode and, if it's exists, put it into the $formID variable
  9. $shortcodeAtt = shortcode_atts( array(
  10. 'id' => false,
  11. ), $atts );
  12. if ($shortcodeAtt != false) {
  13. $formID = $shortcodeAtt['id'];
  14.  
  15. //Setup this form's settings and variables
  16. global $post;
  17. $post = get_post($formID);
  18. setup_postdata($post);
  19.  
  20. //General Variables
  21. $formTitle = get_the_title();
  22. $formSlug = $post->post_name;
  23.  
  24. //Form Submit Behaviour
  25. $onSubmit = get_field('on_form_submit');
  26. if ($onSubmit == 'default') {
  27. //refresh page, display thank you
  28. } elseif ($onSubmit == 'message') {
  29. //refresh page, display custom message
  30. $message = get_field('custom_submission_message');
  31. } elseif ($onSubmit == 'url') {
  32. //send user to this URL
  33. $url = get_field('submission_redirect');
  34. }
  35.  
  36. //Form Submit Action (where to send the form data on submit)
  37. $actionUrl = get_field('form_action_link');
  38.  
  39. //Set value of submit button
  40. if (get_field('submit_value')) {
  41. $submitValue = get_field('submit_value');
  42. } else {
  43. $submitValue = 'Submit';
  44. }
  45.  
  46. //Set ID for form
  47. if (get_field('form_id')) {
  48. $formHtmlId = get_field('form_id');
  49. } else {
  50. $formHtmlId = $formSlug;
  51. }
  52.  
  53. //Set Classes for form
  54. if (get_field('form_classes')) {
  55. $formClasses = get_field('form_classes');
  56. } else {
  57. $formClasses = '';
  58. }
  59.  
  60. //Set any messages to display
  61. $messages = '';
  62. $confirmMessage = get_field('custom_submission_message');
  63. if ($_GET['confirm-message']) {
  64. $messages .= $confirmMessage.'<br />';
  65. } elseif ($_GET['confirm-submit']) {
  66. $messages .= 'Thanks for your submission!';
  67. }
  68.  
  69. //Start the HTML output
  70. $output = '';
  71. //Do some clearing, just in case
  72. $output .= '<div style="clear:both;"></div>';
  73. $output .= '<div class="sdFormWrapper">';
  74. $output .= '<div class="sdFormMessages">';
  75. $output .= $messages;
  76. $output .= '</div>';
  77. $output .= '<form name="'.$formSlug.'" id="'.$formHtmlId.'" class="'.$formClasses.' sd_form" method="post" ';
  78. if (isset($actionUrl)) {
  79. $output .= 'action="'.$actionUrl.'" ';
  80. }
  81. $output .= '>';
  82.  
  83. //We add a hidden field to identify which form is being processed after submit
  84. $output .= '<input type="hidden" name="formID" value="'.$formID.'" />';
  85.  
  86. //This loops through each added field and displays each one
  87. if (have_rows('input_type')) {
  88. while (have_rows('input_type')) { the_row();
  89.  
  90. //We're putting a uniform wrap around each input element for styling
  91. if (get_sub_field('fill_row') == true) {
  92. $fullWidth = 'fullWidth';
  93. } else {
  94. $fullWidth = '';
  95. }
  96. if (get_row_layout() == 'section_header') {
  97. $output .= '<div class="sectionHeader">';
  98. $output .= get_sub_field('header_text');
  99. $output .= '</div>';
  100. } else {
  101.  
  102.  
  103.  
  104. //We turn the field label into a slug with no spaces or special characters
  105. $fieldSlug = sanitize_title(get_sub_field('label'));
  106.  
  107. //Check if this field is required
  108. if (get_sub_field('required') == true) {
  109. $required = 'required';
  110. } else {
  111. $required = '';
  112. }
  113. //Check for custom name
  114. if (get_sub_field('input_name')) {
  115. $name = get_sub_field('input_name');
  116. } else {
  117. $name = $fieldSlug;
  118. }
  119. //Check for custom html id
  120. if (get_sub_field('input_id')) {
  121. $htmlId = get_sub_field('input_id');
  122. } else {
  123. $htmlId = $name;
  124. }
  125. //Check for custom html classes
  126. if (get_sub_field('input_class')) {
  127. $htmlClass = get_sub_field('input_class').' sdForm-input';
  128. } else {
  129. $htmlClass = 'sdForm-input';
  130. }
  131. //Check for icons
  132. if (get_sub_field('icon')) {
  133. $icon = get_sub_field('icon');
  134. } else {
  135. $icon = '';
  136. }
  137. //Generate Placeholder (this is acting as the field label)
  138. $placeholder = get_sub_field('label');
  139.  
  140. //Start input wrap
  141. $output .= '<div title="'.$placeholder.'" class="sdForm-inputWrap '.$fullWidth.'">';
  142.  
  143. $output .= $icon;
  144.  
  145. if (get_row_layout() == 'text') {
  146. $output .= '<input type="text" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required." oninvalid="setCustomValidity(\'\')" />';
  147. } elseif (get_row_layout() == 'textBox') {
  148. $output .= '<textarea name="'.$name.'" rows="4" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required" oninvalid="setCustomValidity(\'\')" ></textarea>';
  149. } elseif (get_row_layout() == 'email') {
  150. $output .= '<input type="email" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required and must be valid." oninvalid="setCustomValidity(\'\')" />';
  151. } elseif (get_row_layout() == 'phone') {
  152. $output .= '<input type="tel" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required." oninvalid="setCustomValidity(\'\')" />';
  153. } elseif (get_row_layout() == 'url') {
  154. $output .= '<input type="url" name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.'" placeholder="'.$placeholder.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required." oninvalid="setCustomValidity(\'\')" />';
  155. } elseif (get_row_layout() == 'checkboxes') {
  156. if (have_rows('checkbox_selections')) {
  157. if ($placeholder != '') {
  158. $output .= '<label for="'.$htmlId.'">'.$placeholder.'</label><br />';
  159. }
  160. while(have_rows('checkbox_selections')) { the_row();
  161. $selection = get_sub_field('checkbox_selection');
  162. $output .= '<input type="checkbox" name="'.$name.'" value="'.$selection.'" id="'.$htmlId.'" class="'.$htmlClass.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required." oninvalid="setCustomValidity(\'\')" />';
  163. $output .= '&nbsp;&nbsp;<label for="'.$htmlId.'">'.$selection.'</label><br />';
  164. }
  165. }
  166. } elseif (get_row_layout() == 'radios') {
  167. if (have_rows('radio_selections')) {
  168. if ($placeholder != '') {
  169. $output .= '<label for="'.$htmlId.'">'.$placeholder.'</label><br />';
  170. }
  171. while(have_rows('radio_selections')) { the_row();
  172. $selection = get_sub_field('radio_selection');
  173. $output .= '<input type="radio" name="'.$name.'" value="'.$selection.'" id="'.$htmlId.'" class="'.$htmlClass.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required." oninvalid="setCustomValidity(\'\')" />';
  174. $output .= '&nbsp;&nbsp;<label for="'.$htmlId.'">'.$selection.'</label><br />';
  175. }
  176. }
  177. } elseif (get_row_layout() == 'select') {
  178. if (get_sub_field('show_label', true)) {
  179. $output .= '<span class="dropdownLabel">'.$placeholder.': </span>';
  180. $showLabel = 'showLabel';
  181. } else {
  182. $showLabel = '';
  183. }
  184. $optionSlug = sanitize_title($selection);
  185. if (have_rows('dropdown_selections')) {
  186.  
  187. $output .= '<select name="'.$name.'" id="'.$htmlId.'" class="'.$htmlClass.' '.$showLabel.'" title="'.$placeholder.'" '.$required.' data-errorMessage="'.$placeholder.' is required." oninvalid="setCustomValidity(\'\')" >';
  188. $output .= '<option selected="select" disabled>';
  189. if ($showLabel != 'showLabel') {
  190. $output .= $placeholder;
  191. }
  192. $output .= '</option>';
  193. while(have_rows('dropdown_selections')) { the_row();
  194. $selection = get_sub_field('dropdown_selection');
  195. $optionSlug = sanitize_title($selection);
  196. $output .= '<option value="'.$optionSlug.'">'.$selection.'</option>';
  197. }
  198. $output .= '</select>';
  199. }
  200. } elseif (get_row_layout() == 'hidden') {
  201. $hiddenVal = get_sub_field('input_value');
  202. $hiddenName = get_sub_field('input_name');
  203. $output .= '<input type="hidden" name="'.$hiddenName.'" value="'.$hiddenVal.'" />';
  204. }
  205. $output .= '<div style="clear:both;"></div>';
  206. $output .= '</div>'; //.sdForm-inputWrap
  207. } //end else (if not a message header)
  208. }//endwhile
  209.  
  210. }
  211.  
  212. $output .= '<input type="submit" name="sdForm-submit" value="'.$submitValue.'" />';
  213.  
  214. $output .= '</form><div style="clear: both;"></div></div>';
  215. $output .= '<script>$("'.$formHtmlId.'").validate();</script>';
  216.  
  217. wp_reset_postdata();
  218.  
  219. } else { //There is no ID set, so we can't show any form
  220. //ERROR!! No form ID specified
  221. }
  222. echo $output;
  223. }
  224.  
  225. add_shortcode('sdForm', 'sdForm');
  226.  
  227. //Enqueue our js file
  228. function sd_form_scripts() {
  229. wp_register_script( 'sdForms', plugin_dir_url( __FILE__ ) . 'js/sdForms.js' );
  230. wp_enqueue_script( 'sdForms' );
  231. }
  232. add_action( 'wp_enqueue_scripts', 'sd_form_scripts' );
  233. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement