<?php
$title = get_the_title();
$url = get_permalink() . '/event';
$dtstamp = get_the_date( 'Ymd\THis\Z' );
$dates = get_field( 'dates' );
$iso_dates = get_iso_dates( $dates[0] );
$description = get_field( 'lead_description' );
//This is the most important coding.
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=loop-event-{$post->post_name}.ics");
echo "BEGIN:VCALENDAR\n";
echo "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n";
echo "VERSION:2.0\n";
echo "BEGIN:VEVENT\n";
echo "DTSTAMP:$dtstamp\n";
echo "DTSTART:{$iso_dates['iso_start_date']}\n";
echo "DTEND:{$iso_dates['iso_end_date']}\n";
echo "SUMMARY:$title\n";
echo "DESCRIPTION:$description\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
