Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Adding ACF fields to an Events Manager booking form
- 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.
- Before you begin, the ACF field group must already exist and contain the fields you need.
- Add the ACF group to the booking form
- 1. Go to Events → Forms Editor.
- 2. Open the booking form you want to update.
- 3. Click Add booking field.
- 4. Set the field type to HTML.
- 5. In Field label, enter the ACF group marker in this format:
- [acf-group:group_your_acf_group_key]
- Example:
- [acf-group:group_6a856b1a2cdc0]
- 6. Leave the EMP field as optional. Do not make this placeholder field required.
- 7. Give the field a simple unique name, such as:
- acf_food_choice
- 8. Click Save Form.
- The technical label is hidden automatically. On the booking form, visitors will see the labels, placeholders, and inputs configured in ACF.
- Find the ACF group key
- The group key always starts with group_, for example:
- group_6a856b1a2cdc0
- It is not the group title. If you cannot find the key, ask your site developer or administrator.
- What happens after someone books
- - The visitor completes the ACF fields as part of the booking form.
- - The answers are saved with the Events Manager booking.
- - In Events → Bookings → Edit Booking, the answers appear under their normal ACF field labels.
- - To change wording, placeholders, options, or order, edit the field group in Custom Fields (ACF)—not in Forms Editor.
- Add more than one ACF group
- Add one HTML placeholder field for each group. Each placeholder needs its own ACF group key:
- [acf-group:group_travel_details]
- [acf-group:group_dietary_requirements]
- */
- **
- * Read [acf-group:group_xxx] from the EMP Form Editor field label.
- */
- function my_emp_get_acf_group_key( $field ) {
- $label = (string) ( $field['field_label'] ?? $field['label'] ?? '' );
- if ( preg_match( '/\[acf-group:(group_[A-Za-z0-9]+)\]/', $label, $matches ) ) {
- return $matches[1];
- }
- return false;
- }
- /**
- * Load ACF JavaScript/CSS required by select, date, repeater, gallery fields, etc.
- */
- add_action( 'wp_enqueue_scripts', function () {
- if ( function_exists( 'acf_enqueue_scripts' ) ) {
- acf_enqueue_scripts();
- }
- }, 20 );
- /**
- * Replace the EMP placeholder field's normal input with an ACF field group.
- *
- * EMP passes: $input, $EM_Form, $field, $value
- */
- add_filter( 'emp_forms_output_field_input', 'my_emp_render_acf_group', 20, 4 );
- function my_emp_render_acf_group( $input, $EM_Form, $field, $value = '' ) {
- $group_key = my_emp_get_acf_group_key( $field );
- if ( ! $group_key || ! function_exists( 'acf_get_fields' ) ) {
- return $input;
- }
- $acf_fields = acf_get_fields( $group_key );
- if ( empty( $acf_fields ) ) {
- return '<p class="emp-acf-error">ACF field group not found.</p>';
- }
- ob_start();
- ?>
- <div class="emp-acf-field-group" data-acf-group="<?php echo esc_attr( $group_key ); ?>">
- <input type="hidden" name="my_emp_acf_groups[]" value="<?php echo esc_attr( $group_key ); ?>">
- <?php
- /*
- * Post ID 0 means a new registration: show ACF defaults/placeholders,
- * without accidentally loading values from the checkout page itself.
- */
- acf_render_fields( $acf_fields, 0 );
- ?>
- </div>
- <?php
- return ob_get_clean();
- }
- /**
- * Optional wrapper class around the EMP field.
- */
- add_filter( 'emp_forms_output_field', 'my_emp_acf_group_wrapper', 20, 4 );
- function my_emp_acf_group_wrapper( $html, $EM_Form, $field, $value = '' ) {
- if ( ! my_emp_get_acf_group_key( $field ) ) {
- return $html;
- }
- // Remove only the EMP label containing [acf-group:group_xxx].
- $html = preg_replace(
- '~<label\b[^>]*>.*?\[acf-group:group_[A-Za-z0-9]+\].*?</label>~is',
- '',
- $html,
- 1
- );
- return '<div class="emp-form-field--acf">' . $html . '</div>';
- }
- /**
- * Store submitted ACF values in Events Manager booking metadata.
- */
- function my_emp_store_acf_booking_values( $booking ) {
- if (
- empty( $_POST['my_emp_acf_groups'] ) ||
- empty( $_POST['acf'] ) ||
- ! is_array( $_POST['acf'] )
- ) {
- return;
- }
- $posted_acf = wp_unslash( $_POST['acf'] );
- $groups = array_unique( (array) wp_unslash( $_POST['my_emp_acf_groups'] ) );
- $saved = array();
- foreach ( $groups as $group_key ) {
- if ( ! preg_match( '/^group_[A-Za-z0-9]+$/', $group_key ) ) {
- continue;
- }
- $fields = acf_get_fields( $group_key );
- if ( empty( $fields ) ) {
- continue;
- }
- foreach ( $fields as $acf_field ) {
- if ( array_key_exists( $acf_field['key'], $posted_acf ) ) {
- $saved[ $group_key ][ $acf_field['key'] ] = $posted_acf[ $acf_field['key'] ];
- }
- }
- }
- if ( $saved ) {
- $booking->booking_meta['booking']['acf'] = $saved;
- }
- }
- /* Standard single-event bookings. */
- add_filter( 'em_booking_get_post', function( $result, $EM_Booking ) {
- my_emp_store_acf_booking_values( $EM_Booking );
- return $result;
- }, 20, 2 );
- /* Multiple Bookings checkout. */
- add_filter( 'em_multiple_booking_get_post', function( $result, $EM_Multiple_Booking ) {
- my_emp_store_acf_booking_values( $EM_Multiple_Booking );
- return $result;
- }, 20, 2 );
- /**
- * Replace the default Booking Details rows with Comment + stored ACF values.
- */
- add_action( 'em_bookings_single_custom', 'my_emp_show_acf_on_booking_edit_screen', 10, 1 );
- function my_emp_show_acf_on_booking_edit_screen( $EM_Booking ) {
- $groups = $EM_Booking->booking_meta['booking']['acf'] ?? array();
- if ( empty( $groups ) || ! is_array( $groups ) || ! function_exists( 'acf_get_fields' ) ) {
- return;
- }
- foreach ( $groups as $group_key => $values ) {
- $fields = acf_get_fields( $group_key );
- if ( empty( $fields ) || ! is_array( $values ) ) {
- continue;
- }
- foreach ( $fields as $acf_field ) {
- if ( ! array_key_exists( $acf_field['key'], $values ) ) {
- continue;
- }
- $raw_value = $values[ $acf_field['key'] ];
- $formatted_value = function_exists( 'acf_format_value' )
- ? acf_format_value( $raw_value, 0, $acf_field )
- : $raw_value;
- ?>
- <tr class="emp-acf-booking-field">
- <th><?php echo esc_html( $acf_field['label'] ); ?></th>
- <td>
- <span class="em-booking-single-info">
- <?php echo wp_kses_post( my_emp_acf_booking_value_html( $formatted_value ) ); ?>
- </span>
- </td>
- </tr>
- <?php
- }
- }
- }
- function my_emp_acf_booking_value_html( $value ) {
- if ( is_bool( $value ) ) {
- return $value ? 'Yes' : 'No';
- }
- if ( is_object( $value ) ) {
- if ( isset( $value->post_title ) ) {
- return esc_html( $value->post_title );
- }
- return esc_html( wp_json_encode( $value ) );
- }
- if ( is_array( $value ) ) {
- $items = array();
- foreach ( $value as $item ) {
- $items[] = my_emp_acf_booking_value_html( $item );
- }
- return implode( ', ', $items );
- }
- return nl2br( esc_html( (string) $value ) );
- }
Advertisement
Add Comment
Please, Sign In to add comment