SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php // in functions.php
  2. /**
  3.  * Adds custom merge tags in Gravity Forms.
  4.  *
  5.  * @since 10/2019
  6.  * @author Alexander Persky
  7.  */
  8. function add_merge_tags($form) { ?>
  9.     <script>
  10.         function add_merge_tags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option) {
  11.             mergeTags['custom'].tags.push(
  12.                 {
  13.                     tag: '{current_day}',
  14.                     label: 'Current Day'
  15.                 },
  16.                 {
  17.                     tag: '{current_time}',
  18.                     label: 'Current Time'
  19.                 }
  20.             );
  21.  
  22.             return mergeTags;
  23.         }
  24.         gform.addFilter('gform_merge_tags', 'add_merge_tags');
  25.     </script>
  26. <?php
  27.     return $form;
  28. }
  29. add_action( 'gform_admin_pre_render', 'add_merge_tags' );
  30.  
  31.  
  32. /**
  33.  * Adds custom merge tag in Gravity Forms.
  34.  *
  35.  * This tag displays the day of the week.
  36.  * Eg: Monday
  37.  *
  38.  * @since 10/2019
  39.  * @author Alexander Persky
  40.  */
  41. function gf_add_current_day($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
  42.     $custom_merge_tag = '{current_day}';
  43.  
  44.     if (strpos($text, $custom_merge_tag) === false) {
  45.         return $text;
  46.     }
  47.  
  48.     $current_day = date('l');
  49.  
  50.     $text = str_replace($custom_merge_tag, $current_day, $text);
  51.  
  52.     return $text;
  53. }
  54. add_filter('gform_replace_merge_tags', 'gf_add_current_day', 10, 7);
  55.  
  56. /**
  57.  * Adds custom merge tag in Gravity Forms.
  58.  *
  59.  * This tag displays the hour of the day in 24hour format with leading zeros.
  60.  * Eg: 00-24
  61.  *
  62.  * @since 10/2019
  63.  * @author Alexander Persky
  64.  */
  65. function gf_add_current_time($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
  66.     $custom_merge_tag = '{current_time}';
  67.  
  68.     if (strpos($text, $custom_merge_tag) === false) {
  69.         return $text;
  70.     }
  71.  
  72.     $current_day = date('H');
  73.  
  74.     $text = str_replace($custom_merge_tag, $current_day, $text);
  75.  
  76.     return $text;
  77. }
  78. add_filter('gform_replace_merge_tags', 'gf_add_current_time', 10, 7);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top