Skip to main content

Saving Custom Event Information

Build an add-on

Part 3 of 11 in the Build an add-on series. Previous: Event meta box. Next: Load event data.

The fields from part 2 are submitted with the event, but Events Manager doesn't know about them. This part stores the ticked styles when an event is saved, copies them to the recurrences of recurring and repeating events, and removes them when an event is deleted.

What you will build​

Three callbacks: one on em_event_save, one on em_recurrence_set_save_recurrences and one on em_event_delete.

Before you start

Complete part 2. The save code relies on the nonce field it adds.

Steps​

  1. Add these lines inside my_em_styles_init():

    add_filter( 'em_event_save', 'my_em_styles_event_save', 10, 2 );
    add_filter( 'em_recurrence_set_save_recurrences', 'my_em_styles_save_recurrences', 10, 2 );
    add_filter( 'em_event_delete', 'my_em_styles_event_delete', 10, 2 );
  2. Add the functions below to the end of the file.

Part 3: saving
/** The style IDs submitted with the event form, or null if our fields were not part of this request. */
function my_em_styles_posted_ids(): ?array {
if ( empty( $_POST['my_em_styles_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['my_em_styles_nonce'] ), 'my_em_styles_event' ) ) {
return null;
}
$posted = array_map( 'absint', (array) ( $_POST['event_styles'] ?? array() ) );
return array_values( array_intersect( $posted, array_keys( my_em_styles_get() ) ) );
}

function my_em_styles_event_save( $result, $EM_Event ) {
$style_ids = my_em_styles_posted_ids();
if ( $style_ids === null || ! $EM_Event->event_id ) {
return $result;
}
my_em_styles_set_event_style_ids( (int) $EM_Event->event_id, $style_ids );
$EM_Event->styles = $style_ids;
return $result;
}

function my_em_styles_save_recurrences( $result, $Recurrence_Set ) {
if ( ! $result ) {
return $result;
}
$style_ids = my_em_styles_posted_ids() ?? my_em_styles_get_event_style_ids( (int) $Recurrence_Set->get_event()->event_id );
foreach ( $Recurrence_Set->get_recurrences() as $recurrence ) {
if ( ! empty( $recurrence['event_id'] ) ) {
my_em_styles_set_event_style_ids( (int) $recurrence['event_id'], $style_ids );
}
}
return $result;
}

function my_em_styles_event_delete( $result, $EM_Event ) {
if ( $result && $EM_Event->event_id ) {
my_em_styles_set_event_style_ids( (int) $EM_Event->event_id, array() );
}
return $result;
}

How it works​

Why the nonce check comes first​

em_event_save is a filter that fires whenever an event has been saved. Its first argument is whether the save succeeded and its second is the EM_Event. It fires for the admin editor and the front-end form, and also for quick edit, the REST API, imports, and once for each recurrence when a recurring event is saved.

Only the first two include your checkboxes. If the callback read $_POST['event_styles'] without checking, a quick edit, which posts no styles, would look the same as someone unticking every box, and the event's styles would be wiped. my_em_styles_posted_ids() returns null unless the request carries a valid my_em_styles_nonce, and the callbacks do nothing in that case.

The function also keeps only IDs that exist in the style list, so a hand-made request can't attach an unknown ID.

Events Manager has already checked that the current user may edit the event by the time em_event_save fires, so the callback doesn't repeat that check.

Why it checks the event ID, not the result​

The callback saves whenever the event has an event_id, even if $result is false. An event that fails validation is still stored as a draft, and keeping the ticked styles means the editor doesn't lose them while fixing the error.

Setting the property​

The line $EM_Event->styles = $style_ids; assigns the whole array at once. Appending to it with $EM_Event->styles[] = 3; would not work: properties that Events Manager doesn't declare are stored through PHP's magic methods, which can't be modified in place.

Recurrences​

A recurring event has a rule, held in a recurrence set, and one event row for each date. Each of those rows has its own event_id, so each needs its own style rows to be found in a search.

em_recurrence_set_save_recurrences fires after a recurrence set has created or updated its events. The callback gets the set, asks it for the parent event with get_event() and for its recurrences with get_recurrences(), and writes the styles to each one. Exclusion sets list dates without events, which is why the loop skips entries with no event_id.

Recurrences are saved before em_event_save fires for the parent event. The callback therefore prefers the IDs in the request, and falls back to what is stored for the parent when the series was saved without the form, for example by the REST API.

Older code

Add-ons written for Events Manager 5 and 6 used the em_event_save_events filter for this. Version 7.5 still fires it for backward compatibility when something is hooked to it, with the arguments $result, $EM_Event, $event_ids, $post_ids, but the source marks it for removal. Use em_recurrence_set_save_recurrences in new code.

Test it​

  1. Edit a single event, tick Formal, and click Update.

  2. Check the stored rows with WP-CLI, or look at the em_meta table in your database tool:

    wp db query "SELECT * FROM $(wp db prefix)em_meta WHERE meta_key = 'event-style'"

    One row has the event's ID in object_id and 1 in meta_value.

  3. In Events then Events, use Quick Edit to change the event's title. Run the query again: the row is still there.

  4. Edit a recurring event, tick a style and update it. The query shows one row for the recurring event and one for each of its dates.

The checkboxes are not ticked when you reopen the event. The data is saved, and the next part loads it.

Next​

Part 4: Load event data