Booking Form Shortcode
-
Hello,
I was wondering if Events Manager has a shortcode for the booking form. This will honestly make my life so much easier as far as adding the form to another page that’s not associated with the plugin.
Thanks,
-
The booking form is part of the single event page. You can use the [event] short code to display a single event. There’s no shortcode to just display the booking form. There is a placeholder for displaying the booking form: #_BOOKINGFORM. You could look at the code in events-manager/classes/em-event.php to see how the placeholder is output and use that information to create your own shortcode.
Thanks so much for this! I will try to figure this out as this will be my first time creating a custom shortcode. Will I need to add any other functions like get_bookings or just stritcly look at the #_BOOKINGFORM placeholder code?
You just need to look at the #_BOOKINGFORM code but first you need to figure out how to set the $EM_Event varianble and then replace $this with $EM_Event. You also need to learn how to create a shortcode: https://www.hostinger.com/tutorials/create-a-shortcode-in-wordpress
You would create a shortcode like: [em-bookingform event=24] where 24 is the event id. Then in the code you could set the $EM_Event variable as follows:
$EM_Event = EM_Events::get(['event' => $event]);
Where $event is set by the parameter value.
So I did this:
<?php function em_get_event() { $EM_Event = EM_Events::get(['event' => $event]); //Setting $EM_EVENT variable //Placeholders preg_match_all("/(#@?_?[A-Za-z0-9_]+)({([^}]+)})?/", $event_string, $placeholders); $replaces = array(); foreach($placeholders[1] as $key => $result) { $match = true; $replace = ''; $full_result = $placeholders[0][$key]; $placeholder_atts = array($result); if( !empty($placeholders[3][$key]) ) $placeholder_atts[] = $placeholders[3][$key]; switch( $result ){ case '#_BOOKINGFORM': if( get_option('dbem_rsvp_enabled')){ if( !defined('EM_XSS_BOOKINGFORM_FILTER') && locate_template('plugins/events-manager/placeholders/bookingform.php') ){ //xss fix for old overridden booking forms add_filter('em_booking_form_action_url','esc_url'); define('EM_XSS_BOOKINGFORM_FILTER',true); } ob_start(); // We are firstly checking if the user has already booked a ticket at this event, if so offer a link to view their bookings. $EM_Booking = $EM_Event->get_bookings()->has_booking(); //count tickets and available tickets $template_vars = array( 'EM_Event' => $EM_Event, 'tickets_count' => count($EM_Event->get_bookings()->get_tickets()->tickets), 'available_tickets_count' => count($EM_Event->get_bookings()->get_available_tickets()), //decide whether user can book, event is open for bookings etc. 'can_book' => is_user_logged_in() || (get_option('dbem_bookings_anonymous') && !is_user_logged_in()), 'is_open' => $EM_Event->get_bookings()->is_open(), //whether there are any available tickets right now 'is_free' => $EM_Event->is_free(), 'show_tickets' => true, 'id' => absint($EM_Event->event_id), 'already_booked' => is_object($EM_Booking) && $EM_Booking->booking_id > 0, 'EM_Booking' => $EM_Event->get_bookings()->get_intent_default(), // get the booking intent if not supplied already ); //if user is logged out, check for member tickets that might be available, since we should ask them to log in instead of saying 'bookings closed' if( !$template_vars['is_open'] && !is_user_logged_in() && $EM_Event->get_bookings()->is_open(true) ){ $template_vars['is_open'] = true; $template_vars['can_book'] = false; $template_vars['show_tickets'] = get_option('dbem_bookings_tickets_show_unavailable') && get_option('dbem_bookings_tickets_show_member_tickets'); } em_locate_template('placeholders/bookingform.php', true, $template_vars); EM_Bookings::enqueue_js(); $replace = ob_get_clean(); } break; case '#_BOOKINGBUTTON': if( get_option('dbem_rsvp_enabled') && $EM_Event->event_rsvp ){ ob_start(); em_locate_template('placeholders/bookingbutton.php', true, array('EM_Event'=>$EM_Event)); $replace = ob_get_clean(); } break; } } } add_shortcode('em-booking-form', 'em_get_event'); ?>
I added the shortcode to a test page and added what I believe is the event id and it’s not showing the booking form
Here’s the code for the shortcode:
function my_booking_form($atts) { $atts = shortcode_atts( array( 'event' => '0' ), $atts, 'em_get_event' ); $id = $atts['event']; $events = EM_Events::get(array('event' => $id)); //Setting $EM_EVENT variable $EM_Event = $events[0]; if( get_option('dbem_rsvp_enabled')){ if( !defined('EM_XSS_BOOKINGFORM_FILTER') && locate_template('plugins/events-manager/placeholders/bookingform.php') ){ //xss fix for old overridden booking forms add_filter('em_booking_form_action_url','esc_url'); define('EM_XSS_BOOKINGFORM_FILTER',true); } ob_start(); // We are firstly checking if the user has already booked a ticket at EM_Event event, if so offer a link to view their bookings. $EM_Booking = $EM_Event->get_bookings()->has_booking(); //count tickets and available tickets $template_vars = array( 'EM_Event' => $EM_Event, 'tickets_count' => count($EM_Event->get_bookings()->get_tickets()->tickets), 'available_tickets_count' => count($EM_Event->get_bookings()->get_available_tickets()), //decide whether user can book, event is open for bookings etc. 'can_book' => is_user_logged_in() || (get_option('dbem_bookings_anonymous') && !is_user_logged_in()), 'is_open' => $EM_Event->get_bookings()->is_open(), //whether there are any available tickets right now 'is_free' => $EM_Event->is_free(), 'show_tickets' => true, 'id' => absint($EM_Event->event_id), 'already_booked' => is_object($EM_Booking) && $EM_Booking->booking_id > 0, 'EM_Booking' => $EM_Event->get_bookings()->get_intent_default(), // get the booking intent if not supplied already ); //if user is logged out, check for member tickets that might be available, since we should ask them to log in instead of saying 'bookings closed' if( !$template_vars['is_open'] && !is_user_logged_in() && $EM_Event->get_bookings()->is_open(true) ){ $template_vars['is_open'] = true; $template_vars['can_book'] = false; $template_vars['show_tickets'] = get_option('dbem_bookings_tickets_show_unavailable') && get_option('dbem_bookings_tickets_show_member_tickets'); } em_locate_template('placeholders/bookingform.php', true, $template_vars); EM_Bookings::enqueue_js(); return ob_get_clean(); } return ""; } add_shortcode('em-booking-form', 'my_booking_form');
If you wanted to display the booking form for the event with an id of 69, you would then use the shortcode: [em-booking-form event=69]
Thanks joneiseman, unfortunately, when I added this code to the script, it gave me this error:
PHP Fatal error: Uncaught Error: Call to a member function get_bookings() on null in /home9/eliminds/staging.elimindset.com/staging/wp-content/themes/eli/includes/shortcodes.php:23 Stack trace: #0 /home9/eliminds/staging.elimindset.com/staging/wp-includes/shortcodes.php(433): my_booking_form(Array, '', 'em-booking-form')
Is this because $EM_Event calling a function from another folder? Also, because we’re not going to use user login for event bookings, can I exclude the member booking and view bookings code?
That error occurred because you didn’t specify a valid event id number in your shortcode. I can add a check for this but I don’t understand the purpose of the booking shortcode. Why do you want a booking shortcode that is separate from the event. Normally the booking form is part of the event.
It’s because I already have an events plugin built in my WordPress and we just needed a way for users to register for certain events and found this plugin. We thought a booking shortcode would fit better since we didn’t want to duplicate what we already have in the front end, unless we have to, but to utilize the tool to place in every event that we want to have a booking form under.
Also, I was able to get the booking form to show successfully. I was looking at the post id and not the event id. Thanks so much for all your help!
- The topic ‘Booking Form Shortcode’ is closed to new replies.