Adding Information to Events on Load
This tutorial forms part of our series of tutorials for creating an custom add-on for Events Manager called “Styles”, where we will be able to select from a list of styles during event registration. In our previous tutorial, we had added some php to save extra event information during event registration.
Loading styles to the EM_Event object
If you decided to create extra information about events and integrate it into Events Manager, you'll certainly want that information available for retrieval when accessing an event's details.
In this case, our events have styles associated with them in the em_meta table, and we want that information loaded when event information is loaded. In order for this to happen, we'll need to create a small function that adds this meta information after an event has finished loading.
function my_em_styles_event_load($EM_Event){
global $wpdb;
$sql = $wpdb->prepare("SELECT meta_value FROM ".EM_META_TABLE." WHERE object_id=%s AND meta_key='event-style'", $EM_Event->event_id);
$EM_Event->styles = $wpdb->get_col($sql, 0);
}
add_action('em_event','my_em_styles_event_load',1,1);
The em_event
action is called just after an event has been called, and passes on the newly created object. This is the perfect opportunity for us to query the meta table and grab any styles associated with that ID.
Now, we can easily access that event's style information by calling the object 'style' property, which will contain a numeric array of all the styles ids of this event. Now that we can add styles and attach them to our events, it's time to start doing something with them!
Side note - Recurring events and further considerations
For the purposes of introducing you to the possibilities of integrating with Events Manager, we'll digress slightly here with some other ideas for approaching this example, since your project may require a different approach too.
In our previous step, we handled saving of events and recurring events. In this example snippet, we're loading the data saved for the specific event being viewed. Whilst this is a perfectly valid way of doing this with certain advantages, we could also opt to share style information across all recurrences rather than saving individual styles for each recurrence. If we wanted to do that, we could ignore the extra function required to save recurring events and we would also reduce the number of records we need to add to our database, since a recurring event with potentially hundreds of recurrences may have only one set of style data.
To load information from the recurring event rather than the individual recurrence, you'd simply add this bit of code just above line 4 above:
if( $EM_Event->is_recurrence() ){ $sql = $wpdb->prepare("SELECT meta_value FROM ".EM_META_TABLE." WHERE object_id=%s AND meta_key='event-style'", $EM_Event->recurrence_id); }
What this does is load style data from the event ID of the recurring event, i.e. the recurrence_id which is stored with each event recurrence.
There are trade-offs here though... mainly being that you cannot save individual style information for specific recurrences. For that reason, you'll need to remove the meta boxes for events that are recurrences so the user is not confused. Also, if the user decides to detach a recurrence from the recurring event, you'll need to handle copying over that information from the recurrence into the individual event (similar to an event save).
Next Step...
In our next tutorial, we're going to make styles accessible as searchable arguments in shortcodes, template tags and EM objects by creating custom event search attributes.