Halloween Sale 15% off Ends in See the plans
PHP

"Add to Calendar" Download for Events

Adds an "Add to calendar" link to single events that downloads an .ics file for Apple Calendar, Outlook and Google Calendar.

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

Code

add_action( 'template_redirect', function () {
    if ( ! isset( $_GET['ical'] ) || ! is_singular( 'event' ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- read-only download.
        return;
    }

    $post  = get_queried_object();
    $tz    = wp_timezone();
    $start = date_create_immutable( (string) get_post_meta( $post->ID, '_event_start', true ), $tz );

    if ( ! $start || ! get_post_meta( $post->ID, '_event_start', true ) ) {
        return;
    }

    $end_raw = (string) get_post_meta( $post->ID, '_event_end', true );
    $end     = $end_raw ? date_create_immutable( $end_raw, $tz ) : false;
    $end     = ( $end && $end > $start ) ? $end : $start->modify( '+1 hour' );
    $utc     = new DateTimeZone( 'UTC' );
    $text    = function ( $value ) {
        return addcslashes( str_replace( array( "\r\n", "\n" ), '\n', wp_strip_all_tags( (string) $value ) ), ',;\\' );
    };
    $where   = trim( get_post_meta( $post->ID, '_event_venue', true ) . ', ' . get_post_meta( $post->ID, '_event_address', true ), ', ' );

    $lines = array(
        'BEGIN:VCALENDAR',
        'VERSION:2.0',
        'PRODID:-//' . $text( get_bloginfo( 'name' ) ) . '//Events//EN',
        'CALSCALE:GREGORIAN',
        'BEGIN:VEVENT',
        'UID:event-' . $post->ID . '@' . wp_parse_url( home_url(), PHP_URL_HOST ),
        'DTSTAMP:' . gmdate( 'Ymd\THis\Z' ),
        'DTSTART:' . $start->setTimezone( $utc )->format( 'Ymd\THis\Z' ),
        'DTEND:' . $end->setTimezone( $utc )->format( 'Ymd\THis\Z' ),
        'SUMMARY:' . $text( get_the_title( $post ) ),
        'URL:' . esc_url_raw( get_permalink( $post ) ),
        'DESCRIPTION:' . $text( get_the_excerpt( $post ) ),
        'LOCATION:' . $text( $where ),
        'END:VEVENT',
        'END:VCALENDAR',
    );

    nocache_headers();
    header( 'Content-Type: text/calendar; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $post->post_name ) . '.ics"' );
    echo implode( "\r\n", $lines ) . "\r\n"; // phpcs:ignore WordPress.Security.EscapeOutput -- calendar format, escaped above.
    exit;
} );

add_filter( 'the_content', function ( $content ) {
    if ( ! is_singular( 'event' ) || ! in_the_loop() || ! is_main_query() || ! get_post_meta( get_the_ID(), '_event_start', true ) ) {
        return $content;
    }

    return $content . '<p class="event-add-to-calendar"><a href="' . esc_url( add_query_arg( 'ical', '1', get_permalink() ) ) . '" rel="nofollow">Add to calendar (.ics)</a></p>';
} );

Notes

Reads the start, end, venue and address saved by the Event Details meta box snippet. Events without an end time last one hour.

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.