ICS file from Gravity Forms Submission in Wordpress

<?php // Escapes a string of characters function escapeString($string) { return preg_replace('/([\/:])/ ','', $string); } //Building Data for our ICS fileqa $summary = 'Requested Appointment'; //text title of the event //Time Date Calculations // we have to convert the DateTime to UTC for the .ics so the calendar software can convert it back to the proper time zone. //input time is LA, Output is UTC $inputTimeZone = new DateTimeZone('America/Los_Angeles'); $outputTimeZone = new DateTimeZone('UTC'); //Date and time from the form $date = htmlspecialchars($_GET["date"]); $time_12_hour = htmlspecialchars($_GET["time"]); //Turn Time into proper 24 hour time, add an hour for endtime $time = date("H:i", strtotime($time_12_hour)); $endtime = date("H:i", strtotime($time) + 3600 ); //ICS Date Format eg 20160201T030400Z or yyyymmdd(T)hhmmss(Z) //Note the use of this variable later on where we build the actual ics file $format = "Ymd\THis\Z"; //Conversion for both start and end times $westcoasttime = new DateTime($date.$time, $inputTimeZone); $datestart = new DateTime($westcoasttime->format("c")); $datestart->setTimezone($outputTimeZone); $wcet = new DateTime($date.$endtime, $inputTimeZone); //west coast end time $dateend = new DateTime($wcet->format("c")); $dateend ->setTimezone($outputTimeZone); // The Rest of the Output for the ICS $address = 'Business Address, 1234 Some Street, Anytown Westside'; // the event's address $uri = htmlspecialchars($_GET["eventuri"]); // - the URL of the event (add http://) $fname = htmlspecialchars($_GET["fname"]); $lname = htmlspecialchars($_GET["lname"]); $vehicle = htmlspecialchars($_GET["vehicle"]); $service = htmlspecialchars($_GET["service"]); //This from the form lets us give each ICS a unique file when it writes to the server //We link to that in our admin notification email $uniqueid = htmlspecialchars($_GET["uid"]); $filename = 'Appointment-Request-'.$uniqueid.'.ics'; // - the name of this file for saving (e.g. my-event-name.ics) // - text description of the event $description = 'Appointement requested by '.escapeString($fname).' '.escapeString($lname).' on '.$date.' at '.$time_12_hour.'. Vehicle: '.escapeString($vehicle).' Service: '.escapeString($service); //The actual ICS we're oputting as the variable $data to plug into our write opperation at the end. $data = 'BEGIN:VCALENDAR VERSION:2.0 CALSCALE:GREGORIAN BEGIN:VEVENT DTEND:'.$dateend->format($format).' UID:'.$uniqueid.' LOCATION:'.escapeString($address).' DESCRIPTION:'.$description.' URL;VALUE=URI:'.$uri.' SUMMARY:'.escapeString($summary).' DTSTART:'.$datestart->format($format).' END:VEVENT END:VCALENDAR'; //Here we write the file file_put_contents ( '/sever/path/domain.com/wp-content/uploads/appointments/'.$filename, $data ); //oh yes, lastly we go on to output the rest of this thank you page too.
A client wanted to generate an .ics (add to outlook calendar) link from an appointment request form on his website. The site is in WordPress and we use gravity forms.

This is the final code I ended up using in the page template for my forms confirmation page. I had to set up the confirmation as a redirect and pass the necessary variables first (anytime you see me using $_GET in the code).

1 Response

Good Morning Liz,
I have been studying your code, I am a newbie when it comes to this so im sorry if i say something silly.
When it comes to the outgoing webhook, do you call a PHP file?
I think my mistake is that I am attempting to add this as a snipit

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.