Skip to main content

PHP Development

Events Manager is object-oriented. An event, a location, a booking and a ticket are each a PHP object, and a matching collection class runs the queries and prints lists. This page is a map of those classes and the functions you call most, checked against Events Manager 7.5.

Before you start

Events Manager 7.5 needs PHP 7.4 or later and WordPress 6.1 or later. Put your code in a plugin or must-use plugin (Where to add PHP), and run it on the events_manager_loaded action or later, or guard it with class_exists( 'EM_Events' ), so it doesn't fail if Events Manager is deactivated.

Print events, calendars and locations​

The three calls below are what the shortcodes and template tags use underneath. Each takes an array of search attributes and returns HTML.

EM_Events::output( $args )​

Returns a list of events.

if ( class_exists( 'EM_Events' ) ) {
echo EM_Events::output( array(
'scope' => 'future',
'limit' => 10,
'orderby' => 'event_start_date,event_start_time',
) );
}

Add a format argument with placeholders to control each item, and format_header and format_footer to wrap the list:

echo EM_Events::output( array(
'limit' => 5,
'format_header' => '<ul class="my-events">',
'format' => '<li>#_EVENTLINK, #_EVENTDATES</li>',
'format_footer' => '</ul>',
) );

EM_Calendar::output( $args )​

Returns a calendar with the events that match the arguments.

if ( class_exists( 'EM_Calendar' ) ) {
echo EM_Calendar::output( array( 'long_events' => 1, 'category' => 'music' ) );
}

EM_Locations::output( $args )​

Returns a list of locations.

if ( class_exists( 'EM_Locations' ) ) {
echo EM_Locations::output( array( 'limit' => 10, 'orderby' => 'location_name' ) );
}

The template tags, such as em_events() and em_get_calendar(), are thin wrappers around these methods for use in theme files.

Get objects instead of HTML​

To work with the data yourself, ask the collection class for objects.

Loop over the next five events
$events = EM_Events::get( array( 'scope' => 'future', 'limit' => 5 ) );
foreach ( $events as $EM_Event ) {
printf(
'<p><a href="%s">%s</a>, %s</p>',
esc_url( $EM_Event->get_permalink() ),
esc_html( $EM_Event->event_name ),
esc_html( $EM_Event->start()->i18n( get_option( 'date_format' ) ) )
);
}

EM_Events::count( $args ) returns the number of matches without loading them. EM_Locations::get() and EM_Bookings::get() work the same way for locations and bookings.

To load one object by id, use the loader functions. They return the object from the cache when it has already been loaded in the request.

FunctionReturns
em_get_event( $id, $search_by = 'event_id' )An EM_Event. Pass 'post_id' as the second argument to load by WordPress post id. A post object also works as the first argument.
em_get_location( $id, $search_by = 'location_id' )An EM_Location.
em_get_booking( $id )An EM_Booking.

A loader always returns an object. Check that the id property is set before using it:

$EM_Event = em_get_event( 123 );
if ( $EM_Event->event_id ) {
echo esc_html( $EM_Event->event_name );
}

Event ids and timeslots​

An event has two ids: event_id, its row in the Events Manager events table, and post_id, its WordPress post. Most Events Manager code uses event_id.

Two cases need care in version 7:

  • A recurrence of a recurring event has an event_id but no post of its own, so post_id is empty. Use $EM_Event->get_recurring_event() to reach the parent event, which holds the post.
  • An event with timeslots is addressed as event_id:timeslot_id, for example 123:456. em_get_event( '123:456' ) loads that timeslot, and $EM_Event->get_event_uid() returns the id in this form.

The main classes​

ClassRepresentsUseful members
EM_EventOne eventevent_id, post_id, event_name, event_owner, start(), end(), get_location(), get_categories(), get_bookings(), get_tickets(), get_permalink(), is_free(), can_manage(), output( $format ), save()
EM_LocationOne locationlocation_id, post_id, location_name, location_town, output( $format )
EM_BookingOne bookingbooking_id, booking_status, booking_spaces, get_event(), get_person(), get_price(), get_tickets_bookings(), approve(), reject(), cancel(), set_status()
EM_BookingsThe bookings of one event (from $EM_Event->get_bookings())get_spaces(), get_available_spaces(), get_booked_spaces(), get_pending_spaces(), has_booking( $user_id )
EM_TicketOne ticketticket_id, ticket_name, is_available()
EM_Events, EM_Locations, EM_CalendarQueries and listsget(), count(), output()

start() and end() return an EM_DateTime object, which extends PHP's DateTime and is set to the event's own timezone. Use ->format(), or ->i18n() for a translated date.

Every object has output( $format ), which replaces placeholders in a string:

echo $EM_Event->output( '#_EVENTNAME at #_LOCATIONNAME, #_EVENTDATES' );

Values you read from properties directly, such as $EM_Event->event_name, are not escaped, so escape them yourself.

Adding your own properties​

You can attach your own data to an event, for example on the em_event action. Assign a whole value at a time:

$EM_Event->my_colors = array( 'red', 'blue' ); // works
$EM_Event->my_colors[] = 'green'; // does not change the stored array

Unknown properties are stored through PHP's magic __set() and __get() methods, so they can't be modified in place. Read the value into a variable, change it, and assign it back.

Global objects​

On a page that belongs to one event, location or booking, Events Manager sets a global for it: $EM_Event, $EM_Location or $EM_Booking. Templates and callbacks can declare the global and use it. $EM_Notices collects error and confirmation messages for the current request. Add to it with $EM_Notices->add_error() or $EM_Notices->add_confirm(), and print it with echo $EM_Notices;.

Database tables​

Events and locations are WordPress posts (post types event and location, available as the constants EM_POST_TYPE_EVENT and EM_POST_TYPE_LOCATION), and each also has a row in an Events Manager table, which is what queries run against. Bookings and tickets exist only in Events Manager tables.

Always use the constants for table names. They include the right prefix, including on multisite with global tables mode, where they point at the network's shared tables.

ConstantTableHolds
EM_EVENTS_TABLEem_eventsEvents, including recurrences
EM_LOCATIONS_TABLEem_locationsLocations
EM_BOOKINGS_TABLEem_bookingsBookings
EM_BOOKINGS_META_TABLEem_bookings_metaExtra data for bookings
EM_TICKETS_TABLEem_ticketsTickets
EM_TICKETS_BOOKINGS_TABLEem_tickets_bookingsThe tickets inside each booking
EM_META_TABLEem_metaGeneral key and value storage by object_id, also usable by add-ons
EM_EVENT_RECURRENCES_TABLEem_event_recurrencesRecurrence rules
EM_EVENT_TIMESLOTS_TABLEem_event_timeslotsTimeslots

EM_MS_GLOBAL is true when global tables mode is on.

Read settings with get_option() as usual. Events Manager's options start with dbem_.

Other ways in​