Skip to content

Instantly share code, notes, and snippets.

@travisfont
Last active February 14, 2017 16:35
Show Gist options
  • Save travisfont/934a85de1992538285bc to your computer and use it in GitHub Desktop.
Save travisfont/934a85de1992538285bc to your computer and use it in GitHub Desktop.
iCalender link integration for WordPress.
<?php
/**
* iCalendar Feed
*/
class ICAL_Export
{
// date format (WARNING: donnot change unless necessary)
const DATE_FORMAT = 'Y-m-d';
private static $feed_url_name = 'my-events';
private static $location;
private static $organizer;
private static $date_start;
private static $date_end;
private static $time_start;
private static $time_end;
private static $date_start_format;
private static $date_end_format;
private static $time_start_format;
private static $time_end_format;
private static $reoccurrence_rule = FALSE;
public static function load(array $parameters)
{
self::$location = $parameters['location'];
self::$organizer = $parameters['organizer'];
self::$date_start = $parameters['date_start'];
self::$date_end = $parameters['date_end'];
self::$time_start = $parameters['time_start'];
self::$date_start_format = $parameters['date_start_format'];
self::$date_end_format = $parameters['date_end_format'];
self::$time_start_format = $parameters['time_start_format'];
self::$time_end_format = $parameters['time_end_format'];
if (isset($parameters['feed_url_name']))
{
self::$feed_url_name = $parameters['feed_url_name'];
}
if (isset($parameters['reoccurrence_rule']))
{
self::$reoccurrence_rule = $parameters['reoccurrence_rule'];
}
add_feed(self::$feed_url_name, array(__CLASS__, 'export_calender'));
}
/**
* Creates an ICAL file of events in the database
*/
public function export_calender()
{
// retrieving the post id from the active url request
$post_id = array_map('strrev', explode('&', strrev($_SERVER['QUERY_STRING'])));
// Query for events (using the event page slug)
$args = array
(
'p' => (int) $post_id[0], // id of a page, post, or custom type
'post_type' => 'any'
);
$my_post = new WP_Query($args);
if ($my_post->have_posts())
{
// re-allocating post variables from parent object
$my_post = $my_post->post;
// Give the ICAL a filename
$filename = self::str_to_url($my_post->post_title);
// Collect output
ob_start();
// File header
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-type: text/calendar');
header('Pragma: 0');
header('Expires: 0');
echo "BEGIN:VCALENDAR";
echo "\nVERSION:2.0";
echo "\nPRODID:-//".get_bloginfo('name')."//NONSGML Events //EN";
echo "\nCALSCALE:GREGORIAN";
echo "\nX-WR-CALNAME:".get_bloginfo('name')." - Events\n";
$meta = get_post_meta($my_post->ID);
echo "\nBEGIN:VEVENT";
echo "\nSUMMARY:".trim(strip_tags($my_post->post_title));
// Universal unique ID
echo "\nUID:".htmlspecialchars_decode($my_post->guid);
// date stamp for now
echo "\nDTSTAMP:".date_i18n('Ymd\THis\Z', time(), TRUE);
// time event created
echo "\nCREATED:".get_post_time('Ymd\THis\Z', TRUE, $my_post->ID);
// event start date
if (self::$time_start == NULL)
{
echo "\nDTSTART;VALUE=DATE:".DateTime::createFromFormat(self::$date_start_format, $meta[self::$date_start][0])->format('Ymd');
}
else
{
echo "\nDTSTART:".DateTime::createFromFormat(self::$date_start_format.' '.self::$time_end_format, $meta[self::$date_start][0].' '.$meta[self::$time_start][0])->format('Ymd\THis\Z');
}
// event end date
if (self::$time_end == NULL)
{
echo "\nDTEND;VALUE=DATE:".DateTime::createFromFormat(self::$date_end_format, $meta[self::$date_end][0])->format('Ymd');
}
else
{
echo "\nDTEND:".DateTime::createFromFormat(self::$date_end_format.' '.self::$time_end_format, $meta[self::$date_end][0].' '.$meta[self::$time_end][0])->format('Ymd\THis\Z');
}
// event reoccurrence rule
if (self::$reoccurrence_rule)
{
echo "\nRRULE:".self::$reoccurrence_rule;
}
// event description (br and p replace with newline and removing all htmk)
echo "\nDESCRIPTION:".substr(trim(preg_replace('#\s*\[.+\]\s*#U', ' ', str_replace('&nbsp;', '', strip_tags(str_replace(array('<br>', '<br/>', '<p>', '</p>'), "/n", trim($my_post->post_content)))))), 0, 255).'...';
// event location
echo "\nLOCATION:".str_replace(',', '\,', $meta[self::$location][0]).'\, Luxembourg';
// event organiser
echo "\nORGANIZER: ".ucwords(strtolower($meta[self::$organizer][0]));
echo "\nEND:VEVENT\n";
}
echo "\nEND:VCALENDAR";
// Collect output and echo
$eventsical = ob_get_contents();
ob_end_clean();
echo $eventsical;
exit;
}
/**
* Creates an ICAL file of events in the database
* @param string text - the string text to convert
*/
private static function str_to_url($text)
{
return (string) strtolower(
str_replace('--', '-',
str_replace(array('+', ' '), '-',
htmlentities('Event-'.
str_replace(array('&', '+', '?', ',', '@', '!', '#', '$', '%', '^', '*', '=', '.'), '',
strip_tags(remove_accents($text))).'-'.date(self::DATE_FORMAT).'.ics'))));
}
}
ICAL_Export::load(array
(
'location' => 'event_location', // venue and/or address
'organizer' => 'event_organiser_name', // company or organizator
'date_start' => 'start_date', // event start date
'date_end' => 'end_date', // event end date
'time_start' => NULL, // event start time
'time_end' => NULL, // event end time
'date_start_format' => 'Ymd',
'date_end_format' => 'Ymd',
'time_start_format' => NULL,
'time_end_format' => NULL
));
@travisfont
Copy link
Author

travisfont commented Feb 14, 2017

Applied to the WP functions "functions.php" which is ideally located within your currently used 'theme' main folder. Then add <a href="<?php echo get_feed_link('my-events').'&'.get_the_ID(); ?>">LINK HERE</a> to any template HTML page where the iCal link is located.

If the above link link code doesn't work then try: <a href="/?feed=my-events&<?php echo get_the_ID();?>">LINK HERE</a>..

@travisfont
Copy link
Author

However, all in all, if you want full calendar functionality, I would consider and invest in looking to http://addtocalendar.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment