Saving Custom Event Information

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 showed you how to add a meta box with extra form fields for users to add customized information about their event. The following tutorial will show how to save this information whilst the normal event data is updated/added.

function my_em_styles_event_save($result,$EM_Event){
	global $wpdb;
	$my_em_styles = (is_array(get_option('my_em_styles'))) ? get_option('my_em_styles'):array();
	if( $result && !empty($_POST['event_styles']) ){
		$ids_to_add = array();
		$EM_Event->styles = array();
		foreach( $_POST['event_styles'] as $style_id ){
			if( array_key_exists($style_id, $my_em_styles) ){
				$ids_to_add[] = "({$EM_Event->id}, 'event-style', '$style_id')";
				$EM_Event->styles[] = $style_id;
		//First delete any old saves
		$wpdb->query("DELETE FROM ".EM_META_TABLE." WHERE object_id='{$EM_Event->id}' AND meta_key='event-style'");
		if( count($ids_to_add) > 0 ){
			$wpdb->query("INSERT INTO ".EM_META_TABLE." (object_id, meta_key, meta_value) VALUES ".implode(',',$ids_to_add));
	return $result;

The first line adds a filter to the em_event_save filter, which is called every time an event has been saved, and also passes on the result of the save and the EM_Event object being saved.

The fourth line obtains the styles array from the database, or loads up an empty array. The following line checks to make sure that event styles have in fact been submitted and that the $result object is true, meaning the EM_Event was successfully saved.

We then loop through the _POST[‘event_styles’] array, which contains the user’s selected styles and add the styles that exist in our $styles array to this event. Note that we’re just adding style ID numbers, as we can obtain style information by referring to eh $my_em_styles array in the future (or obtain it again).

We’ll firstly delete all previous events and finally save the new style ids to the wp_em_meta table (wp_ prefix may differ on your install). We will be storing our style associations to this table because with an event ID, it is very easy to retrieve all script IDs that are associated with that event.

Now, both the database and the object are updated with the latest style values and can be obtained in other parts of your site! However, before we can access the event styles information, we need to add styles data to events on load, as they are not automatically loaded.