Last active
December 14, 2015 20:26
-
-
Save maplesyrupsucker/9dd9b631b8398293e091 to your computer and use it in GitHub Desktop.
cpt-events-ical.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// see: http://www.noeltock.com/web-design/wordpress/how-to-ical-with-custom-post-types/ for more info | |
//add to functions.php: load_template( dirname( __FILE__ ) . '/lib/cpt-events-ical.php' ); | |
function tf_events_ical() { | |
// - start collecting output - | |
ob_start(); | |
// - file header - | |
header('Content-type: text/calendar'); | |
header('Content-Disposition: attachment; filename="ical.ics"'); | |
// - content header - | |
?> | |
BEGIN:VCALENDAR | |
VERSION:2.0 | |
PRODID:-//<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>//NONSGML Events //EN | |
X-WR-CALNAME:<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?> – Events | |
X-ORIGINAL-URL:<?php echo esc_url( home_url( '/' ) ); ?>/?feed=tf-events-ical | |
X-WR-CALDESC:<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?> – Upcoming Events | |
CALSCALE:GREGORIAN | |
<?php | |
// - grab date barrier - | |
$today6am = strtotime('today 6:00') + ( get_option( 'gmt_offset' ) * 3600 ); | |
//removed $limit in $querystr and set to 500 for the time being (originally: $limit = get_option('pubforce_rss_limit');) | |
// - query - | |
global $wpdb; | |
$querystr = " | |
SELECT * | |
FROM $wpdb->posts wposts, $wpdb->postmeta metastart, $wpdb->postmeta metaend | |
WHERE (wposts.ID = metastart.post_id AND wposts.ID = metaend.post_id) | |
AND (metaend.meta_key = 'event_end' ) | |
AND metastart.meta_key = 'event_end' | |
AND wposts.post_type = 'base_events' | |
AND wposts.post_status = 'publish' | |
ORDER BY metastart.meta_value ASC LIMIT 500 | |
"; | |
$events = $wpdb->get_results($querystr, OBJECT); | |
// - loop - | |
if ($events): | |
global $post; | |
foreach ($events as $post): | |
setup_postdata($post); | |
// - custom variables - | |
$custom = get_post_custom(get_the_ID()); | |
$sd = $custom["event_start"][0]; | |
$ed = $custom["event_end"][0]; | |
// - grab gmt for start - | |
$gmts = date('Y-m-d H:i:s', $sd); | |
$gmts = get_gmt_from_date($gmts); // this function requires Y-m-d H:i:s | |
$gmts = strtotime($gmts); | |
// - grab gmt for end - | |
$gmte = date('Y-m-d H:i:s', $ed); | |
$gmte = get_gmt_from_date($gmte); // this function requires Y-m-d H:i:s | |
$gmte = strtotime($gmte); | |
// - Set to UTC ICAL FORMAT - | |
$stime = date('Ymd\THis\Z', $gmts); | |
$etime = date('Ymd\THis\Z', $gmte); | |
// - item output - | |
?> | |
BEGIN:VEVENT | |
DTSTART:<?php echo $stime . "\n";?> | |
DTEND:<?php echo $etime . "\n";?> | |
SUMMARY:<?php echo the_title() . "\n"; ?> | |
DESCRIPTION:<?php echo substr(get_the_excerpt(), 0,50) . "\n"; ?> | |
END:VEVENT | |
<?php | |
endforeach; | |
else : | |
endif; | |
?> | |
END:VCALENDAR | |
<?php | |
// - full output - | |
$tfeventsical = ob_get_contents(); | |
ob_end_clean(); | |
echo $tfeventsical; | |
} | |
function add_tf_events_ical_feed () { | |
// - add it to WP RSS feeds - | |
add_feed('tf-events-ical', 'tf_events_ical'); | |
} | |
add_action('init','add_tf_events_ical_feed'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment