Events are a very popular custom posts type in WordPress and customers need to export an event to their calendar. It can be Apple iCal or Google Calendar. 

In this article, I will describe how to add events to your calendar (either Apple iCal or Google Calendar).

How to create Google Calendar link?

We will use PHP to implement the script. 

Create a function make_google_calendar_link, which has the following parameters –name, begin, end, location, details.

Name – is the name of event.
Begin – is the start date of event.
End – is the end date of event.
Location – is the location of event.
Details – is the description of event.

				
					function make_google_calendar_link($name, $begin, $end, $location, $details) {
	$params = array('&dates=', '/', '&location=', '&details=', '&sf=true&output=xml');
	$url = 'https://www.google.com/calendar/render?action=TEMPLATE&text=';
	$arg_list = func_get_args();
	for ($i = 0; $i < count($arg_list); $i++) {
		$current = $arg_list[$i];
		if(is_int($current)) {
			$t = new DateTime('@' . $current, new DateTimeZone('UTC'));
			if($i == 2){
				$current = $t->format('YmdTHis');
			}else{
				$current = $t->format('YmdTHisZ');
			}
			unset($t);
		}
		else {
			$current = urlencode($current);
		}
		$url .= (string) $current . $params[$i];
	}
	return $url;
}
				
			

If we call this function like here:

				
					$linkGoogle = make_google_calendar_link('Event name', strtotime('26 Oct, 10:30 pm'), strtotime('26 Oct, 11:30 pm'), "Khmelnytskyi", 'Test description');
				
			

We will get a link to the Google Calendar:

				
					https://www.google.com/calendar/render?action=TEMPLATE&text=Event+name&dates=20231026T223000Z/20231026T233000&location=Khmelnytskyi&details=Test+description&sf=true&output=xml
				
			

Create the HTML tag and add the URL to the “href” attribute of the “<a>” tag. Example:

				
					<a href="<?php echo $linkGoogle; ?>" target="_blank">Add to Google Calendar</a>
				
			

If we click on the link, we will be directed to a Google Calendar form with our data.

As you see on screenshot It works: now user can add event to his Google calendar:)

How to create Apple iCal link?

The process of exporting an event to Apple’s calendar is a bit more complicated. To do this, we need a third-party library.

You need to download Zap Calendar iCalendar Library

After that let’s create a PHP script

Add a function makeAppleCalendar, which has the following parameters – name, begin, end,  details,  namefile.

Name – is the name of event.
Begin – is the start date of event.
End – is the end date of event.
Details – is the description of event.
Namefile – is the name of the file to be downloaded.

				
					function makeAppleCalendar($name, $begin, $end, $details, $namefile){
    require('./icalendar-master/zapcallib.php');//include library
    $icalobj = new ZCiCal();
    $eventobj = new ZCiCalNode("VEVENT", $icalobj->curnode);
    $eventobj->addNode(new ZCiCalDataNode("SUMMARY:" . $name));
    $eventobj->addNode(new ZCiCalDataNode("PRODID:-//WordPress - MECv6.6.5//EN"));
    $eventobj->addNode(new ZCiCalDataNode("DTSTART:" . ZCiCal::fromSqlDateTime($begin) . 'Z'));
    $eventobj->addNode(new ZCiCalDataNode("DTEND:" . ZCiCal::fromSqlDateTime($end) . 'Z'));
    $uid = date('Y-m-d-H-i-s') . "@demo.icalendar.org";
    $eventobj->addNode(new ZCiCalDataNode("DTSTAMP:" . ZCiCal::fromSqlDateTime()));
    $eventobj->addNode(new ZCiCalDataNode("DESCRIPTION:" . $details));
    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=$namefile.ics");
    echo $icalobj->export();
}
				
			

If we call this function, example:

				
					makeAppleCalendar("Event name", "2023-11-11 22:30:00", "2023-11-11 23:30:00", "Event description", "calendar");
				
			

We will get a file calendar.ics
When we open file, Apple Calendar will open with a form for adding our event