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 (Outlook, Google Calendar, 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!

wordpress ics calendar

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;

[mt]

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?

Jany Martelli · Shambix

Scritto dal team di ingegneri Shambix: app su misura, agenti AI, WordPress e WooCommerce, cloud e AI privata dal 2009.

Jany Martelli progetta i sistemi dietro i business digitali: architetture cloud, piattaforme e-commerce e agenti AI che fanno lavoro reale. Oltre 17 anni e più di 250 progetti con Shambix, per team che vanno da Xiaomi a Hilton. Una formazione in informatica, l'insegnamento e Patcherly, l'AI che corregge da sola i bug in produzione.

WordPress e WooCommerce

Hai un Progetto in mente? Sviluppiamolo insieme.

Raccontaci i tuoi obiettivi e il tuo budget. Da una sistemata veloce a WordPress fino a una piattaforma su misura, ti diremo cosa è realistico. Al resto pensiamo noi.

  • Ogni richiesta viene letta da un ingegnere senior
  • Se non siamo i partner giusti, ve lo diciamo
  • info@shambix.com
Nome
Lavoriamo con budget flessibili e di ogni tipo, per piccoli e grandi progetti. Serve a orientare meglio la soluzione più adatta al tuo progetto.

Di solito rispondiamo entro 24 ore. Niente newsletter, niente spam.