Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.