Dynamic iCal generator outside/inside wordpress
So following on from this post: https://stackoverflow.com/q/1463480/1086990 I was wondering how exactly it would work integrating it into Wordpress.
I have a custom post type setup, and so far I have tried both linking to a "ical.php" with the above link code (and changing the title into the_title()
and the dates into GMT versions of my date(..)
code). None work, because it can find the get_post_custom_values()
which is obvious since not in WP loop, or a WP associated file.
Next I tried adding the headers
into the header.php
WP file, and adding a if(is_singular("event"))
, but the headers auto download. If I can stop the auto download then I'm ace, otherwise I come here for help!
Is there any reasonable way that is dynamically able to be populated via WP and only download when the link is clicked, not automatically?
I would host everything, but the events are going to be so often, that making and hosting would be a waste also is there maybe a href generator like Google Calendar that would automatically work?
EDIT (My working code):
Custom Taxonomy Loop
form action="?php echo get_feed_link('calendar-event'); ?" method="post"
input hidden="hidden" name="eventID" value="?php the_ID(); ?"
button title="Add to iCal" type="submit" name="iCalForm"iCal/button
/form
iCal.php
I included this into the functions.php
via a require()
.
?php //Event ICAL feed
class SH_Event_ICAL_Export {
public function load() { add_feed('calendar-event', array(__CLASS__,'export_events')); }
// Creates an ICAL file of events in the database
public function export_events(){
//Give the iCal export a filename
$filename = urlencode( 'event-ical-' . date('Y-m-d') . '.ics' );
//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");
?
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//?php echo get_bloginfo('name'); ? //NONSGML Events //EN
CALSCALE:GREGORIAN
X-WR-CALNAME:?php echo get_bloginfo('name');?: Events
?php // Query for events
if(isset($_POST['iCalForm'])) {
$post_ID = $_POST['eventID'];
$events = new WP_Query(array(
'p' = $post_ID,
'post_type' = 'event' //Or whatever the name of your post type is
));
if($events-have_posts()) : while($events-have_posts()) : $events-the_post();
$uid = get_the_ID(); // Universal unique ID
$dtstamp = date_i18n('Ymd\THis\Z',time(), true); // Date stamp for now.
$created_date = get_post_time('Ymd\THis\Z', true, get_the_ID() ); // Time event created
// Other pieces of "get_post_custom_values()" that make up for the StartDate, EndDate, EventOrganiser, Location, etc.
// I also had a DeadlineDate which I included into the BEGIN:VALARM
// Other notes I found while trying to figure this out was for All-Day events, you just need the date("Ymd"), assuming that Apple iCal is your main focus, and not Outlook, or others which I haven't tested :]
?
BEGIN:VEVENT
CREATED:?php echo $created_date;?
UID:?php echo $uid;?
DTEND;VALUE=DATE:?php echo $end_date; ?
TRANSP:OPAQUE
SUMMARY:?php echo $organiser; ?
DTSTART;VALUE=DATE:?php echo $start_date ; ?
DTSTAMP:?php echo $dtstamp;?
LOCATION:?php echo $location;?
ORGANIZER:?php echo $organiser;?
URL;VALUE=URI:?php echo "http://".$url; ?
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;VALUE=DATE-TIME:?php echo $dLine; ?
DESCRIPTION:Closing submission day of films for ?php echo $organiser; ?! Enter quickly!
END:VALARM
END:VEVENT
?php endwhile; endif; } ?
END:VCALENDAR
?php //Collect output and echo
$eventsical = ob_get_contents();
ob_end_clean();
echo $eventsical;
exit();
}
} // end class
SH_Event_ICAL_Export::load();
?
Hope this serves well to whoever needs it, as it is a fantastic piece of work, so again thanks Stephen Harris who helped more than the question, and additionally without suggesting I use his plugin.
Topic calendar outside-wordpress custom-post-types Wordpress
Category Web