Create .ics calendar files from WordPress posts & custom post types

For websites that feature events, either through plugin or custom post type, it might be important to allow user to download individual.ics calendar files.
ICS files can be opened in pretty much any calendar management app or software, like Outlook, Google Calendar and iCalendar.
Often, plugins provide the .ics generation functionality built-in, other times they require that you purchase an add-on… now you can do it yourself!

Copy and paste this script in your theme’s functions.php file and edit the parts that you need to customize:

Make sure that you read carefully through the code, especially the part with the date vars, location, organizer etc. so that your events are fully represented in the users’ calendar.

I personally like to use Types or Custom Post Type UI, to manage custom post types, custom taxonomies and custom fields, so a practical example in that case would be:

$start_date = date_i18n("Ymd\THis\Z", get_post_meta( get_the_ID(), 'wpcf-start_date', true ));
$end_date = date_i18n('Ymd\THis\Z', get_post_meta( get_the_ID(), 'wpcf-end_date', true));
$deadline = date_i18n('Ymd\THis\Z', get_post_meta( get_the_ID(), 'wpcf-deadline_date', true)-24, true);
$location = types_render_field("location", array('output' => 'raw'));
$city = types_render_field("city", array('output' => 'raw'));
$street = types_render_field("street", array('output' => 'raw'));
$address = $location.' - '.$street.', '.$city;

So, once looping through your posts, all you have to add is a link or button to the custom endpoint, carrying the post ID in the url parameter, like this:

<a href="<?php echo get_feed_link('calendar'); ?>?id=<?php echo get_the_ID(); ?>"> Download .ics </a>

The user will then be able to download and open the .ics file in whatever calendar app / software they want.

Now, why not pairing this cool feature with a handy weekly calendar for WordPress?