Add Online and Status Details to Event Schema
Enriches The Events Calendar's JSON-LD with eventStatus and eventAttendanceMode, marking events that have an event website but no venue as online events, as Google recommends.
Opens WPIDE on
Code
add_filter( 'tribe_json_ld_event_object', function ( $data, $args, $post ) {
if ( ! is_object( $data ) || ! $post ) {
return $data;
}
$statuses = array(
'cancelled' => 'https://schema.org/EventCancelled',
'postponed' => 'https://schema.org/EventPostponed',
'rescheduled' => 'https://schema.org/EventRescheduled',
);
$status = strtolower( (string) get_post_meta( $post->ID, 'event_status', true ) );
$data->eventStatus = isset( $statuses[ $status ] ) ? $statuses[ $status ] : 'https://schema.org/EventScheduled';
$has_venue = (int) get_post_meta( $post->ID, '_EventVenueID', true ) > 0;
$url = get_post_meta( $post->ID, '_EventURL', true );
if ( ! $has_venue && $url ) {
$data->eventAttendanceMode = 'https://schema.org/OnlineEventAttendanceMode';
$data->location = (object) array( '@type' => 'VirtualLocation', 'url' => esc_url_raw( $url ) );
} else {
$data->eventAttendanceMode = 'https://schema.org/OfflineEventAttendanceMode';
}
return $data;
}, 10, 3 );
Notes
Add a custom field named event_status with the value cancelled, postponed or rescheduled to mark changed events.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.