eventsmanager

Integrate with ACF

Aug 19th, 2026
2,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Adding ACF fields to an Events Manager booking form
  6. Use this when you want to add a pre-built ACF field group—such as dietary requirements, travel details, or preferences—to a booking form.
  7. Before you begin, the ACF field group must already exist and contain the fields you need.
  8. Add the ACF group to the booking form
  9. 1. Go to Events → Forms Editor.
  10. 2. Open the booking form you want to update.
  11. 3. Click Add booking field.
  12. 4. Set the field type to HTML.
  13. 5. In Field label, enter the ACF group marker in this format:
  14. [acf-group:group_your_acf_group_key]
  15. Example:
  16. [acf-group:group_6a856b1a2cdc0]
  17. 6. Leave the EMP field as optional. Do not make this placeholder field required.
  18. 7. Give the field a simple unique name, such as:
  19. acf_food_choice
  20. 8. Click Save Form.
  21. The technical label is hidden automatically. On the booking form, visitors will see the labels, placeholders, and inputs configured in ACF.
  22. Find the ACF group key
  23. The group key always starts with group_, for example:
  24. group_6a856b1a2cdc0
  25. It is not the group title. If you cannot find the key, ask your site developer or administrator.
  26. What happens after someone books
  27. - The visitor completes the ACF fields as part of the booking form.
  28. - The answers are saved with the Events Manager booking.
  29. - In Events → Bookings → Edit Booking, the answers appear under their normal ACF field labels.
  30. - To change wording, placeholders, options, or order, edit the field group in Custom Fields (ACF)—not in Forms Editor.
  31. Add more than one ACF group
  32. Add one HTML placeholder field for each group. Each placeholder needs its own ACF group key:
  33. [acf-group:group_travel_details]
  34. [acf-group:group_dietary_requirements]
  35.  
  36.  
  37. */
  38.  
  39. **
  40. * Read [acf-group:group_xxx] from the EMP Form Editor field label.
  41. */
  42. function my_emp_get_acf_group_key( $field ) {
  43. $label = (string) ( $field['field_label'] ?? $field['label'] ?? '' );
  44.  
  45. if ( preg_match( '/\[acf-group:(group_[A-Za-z0-9]+)\]/', $label, $matches ) ) {
  46. return $matches[1];
  47. }
  48.  
  49. return false;
  50. }
  51.  
  52.  
  53. /**
  54. * Load ACF JavaScript/CSS required by select, date, repeater, gallery fields, etc.
  55. */
  56. add_action( 'wp_enqueue_scripts', function () {
  57. if ( function_exists( 'acf_enqueue_scripts' ) ) {
  58. acf_enqueue_scripts();
  59. }
  60. }, 20 );
  61.  
  62.  
  63. /**
  64. * Replace the EMP placeholder field's normal input with an ACF field group.
  65. *
  66. * EMP passes: $input, $EM_Form, $field, $value
  67. */
  68. add_filter( 'emp_forms_output_field_input', 'my_emp_render_acf_group', 20, 4 );
  69. function my_emp_render_acf_group( $input, $EM_Form, $field, $value = '' ) {
  70. $group_key = my_emp_get_acf_group_key( $field );
  71.  
  72. if ( ! $group_key || ! function_exists( 'acf_get_fields' ) ) {
  73. return $input;
  74. }
  75.  
  76. $acf_fields = acf_get_fields( $group_key );
  77.  
  78. if ( empty( $acf_fields ) ) {
  79. return '<p class="emp-acf-error">ACF field group not found.</p>';
  80. }
  81.  
  82. ob_start();
  83. ?>
  84. <div class="emp-acf-field-group" data-acf-group="<?php echo esc_attr( $group_key ); ?>">
  85. <input type="hidden" name="my_emp_acf_groups[]" value="<?php echo esc_attr( $group_key ); ?>">
  86.  
  87. <?php
  88. /*
  89. * Post ID 0 means a new registration: show ACF defaults/placeholders,
  90. * without accidentally loading values from the checkout page itself.
  91. */
  92. acf_render_fields( $acf_fields, 0 );
  93. ?>
  94. </div>
  95. <?php
  96.  
  97. return ob_get_clean();
  98. }
  99.  
  100.  
  101. /**
  102. * Optional wrapper class around the EMP field.
  103. */
  104. add_filter( 'emp_forms_output_field', 'my_emp_acf_group_wrapper', 20, 4 );
  105. function my_emp_acf_group_wrapper( $html, $EM_Form, $field, $value = '' ) {
  106. if ( ! my_emp_get_acf_group_key( $field ) ) {
  107. return $html;
  108. }
  109.  
  110. // Remove only the EMP label containing [acf-group:group_xxx].
  111. $html = preg_replace(
  112. '~<label\b[^>]*>.*?\[acf-group:group_[A-Za-z0-9]+\].*?</label>~is',
  113. '',
  114. $html,
  115. 1
  116. );
  117.  
  118. return '<div class="emp-form-field--acf">' . $html . '</div>';
  119. }
  120.  
  121. /**
  122. * Store submitted ACF values in Events Manager booking metadata.
  123. */
  124. function my_emp_store_acf_booking_values( $booking ) {
  125. if (
  126. empty( $_POST['my_emp_acf_groups'] ) ||
  127. empty( $_POST['acf'] ) ||
  128. ! is_array( $_POST['acf'] )
  129. ) {
  130. return;
  131. }
  132.  
  133. $posted_acf = wp_unslash( $_POST['acf'] );
  134. $groups = array_unique( (array) wp_unslash( $_POST['my_emp_acf_groups'] ) );
  135. $saved = array();
  136.  
  137. foreach ( $groups as $group_key ) {
  138. if ( ! preg_match( '/^group_[A-Za-z0-9]+$/', $group_key ) ) {
  139. continue;
  140. }
  141.  
  142. $fields = acf_get_fields( $group_key );
  143.  
  144. if ( empty( $fields ) ) {
  145. continue;
  146. }
  147.  
  148. foreach ( $fields as $acf_field ) {
  149. if ( array_key_exists( $acf_field['key'], $posted_acf ) ) {
  150. $saved[ $group_key ][ $acf_field['key'] ] = $posted_acf[ $acf_field['key'] ];
  151. }
  152. }
  153. }
  154.  
  155. if ( $saved ) {
  156. $booking->booking_meta['booking']['acf'] = $saved;
  157. }
  158. }
  159.  
  160.  
  161. /* Standard single-event bookings. */
  162. add_filter( 'em_booking_get_post', function( $result, $EM_Booking ) {
  163. my_emp_store_acf_booking_values( $EM_Booking );
  164. return $result;
  165. }, 20, 2 );
  166.  
  167.  
  168. /* Multiple Bookings checkout. */
  169. add_filter( 'em_multiple_booking_get_post', function( $result, $EM_Multiple_Booking ) {
  170. my_emp_store_acf_booking_values( $EM_Multiple_Booking );
  171. return $result;
  172. }, 20, 2 );
  173.  
  174. /**
  175. * Replace the default Booking Details rows with Comment + stored ACF values.
  176. */
  177. add_action( 'em_bookings_single_custom', 'my_emp_show_acf_on_booking_edit_screen', 10, 1 );
  178.  
  179. function my_emp_show_acf_on_booking_edit_screen( $EM_Booking ) {
  180.  
  181.  
  182. $groups = $EM_Booking->booking_meta['booking']['acf'] ?? array();
  183.  
  184. if ( empty( $groups ) || ! is_array( $groups ) || ! function_exists( 'acf_get_fields' ) ) {
  185. return;
  186. }
  187.  
  188. foreach ( $groups as $group_key => $values ) {
  189. $fields = acf_get_fields( $group_key );
  190.  
  191. if ( empty( $fields ) || ! is_array( $values ) ) {
  192. continue;
  193. }
  194.  
  195. foreach ( $fields as $acf_field ) {
  196. if ( ! array_key_exists( $acf_field['key'], $values ) ) {
  197. continue;
  198. }
  199.  
  200. $raw_value = $values[ $acf_field['key'] ];
  201.  
  202. $formatted_value = function_exists( 'acf_format_value' )
  203. ? acf_format_value( $raw_value, 0, $acf_field )
  204. : $raw_value;
  205. ?>
  206. <tr class="emp-acf-booking-field">
  207. <th><?php echo esc_html( $acf_field['label'] ); ?></th>
  208. <td>
  209. <span class="em-booking-single-info">
  210. <?php echo wp_kses_post( my_emp_acf_booking_value_html( $formatted_value ) ); ?>
  211. </span>
  212. </td>
  213. </tr>
  214. <?php
  215. }
  216. }
  217. }
  218.  
  219. function my_emp_acf_booking_value_html( $value ) {
  220. if ( is_bool( $value ) ) {
  221. return $value ? 'Yes' : 'No';
  222. }
  223.  
  224. if ( is_object( $value ) ) {
  225. if ( isset( $value->post_title ) ) {
  226. return esc_html( $value->post_title );
  227. }
  228.  
  229. return esc_html( wp_json_encode( $value ) );
  230. }
  231.  
  232. if ( is_array( $value ) ) {
  233. $items = array();
  234.  
  235. foreach ( $value as $item ) {
  236. $items[] = my_emp_acf_booking_value_html( $item );
  237. }
  238.  
  239. return implode( ', ', $items );
  240. }
  241.  
  242. return nl2br( esc_html( (string) $value ) );
  243. }
Advertisement
Add Comment
Please, Sign In to add comment