下面是这篇文章:https://stackoverflow.com/q/1463480/1086990 我想知道将其集成到Wordpress中究竟会如何工作。
我有一个自定义的帖子类型设置,到目前为止,我已经尝试使用上述链接代码链接到“ical.php”(并将标题改为the_title()
和GMT版本的日期date(..)
代码)。没有工作,因为它可以找到get_post_custom_values()
这一点很明显,因为它不在WP循环或WP关联文件中。
接下来,我尝试添加headers
进入header.php
WP文件,并添加if(is_singular("event"))
, 但标题会自动下载。如果我可以停止自动下载,那么我就是ace,否则我会来这里寻求帮助!
是否有任何合理的方式可以通过WP和only 单击链接时下载,而不是自动下载?
我会主持所有的活动,但活动太频繁了,制作和主持都是浪费also 是否有像谷歌日历这样的href生成器可以自动工作?
<小时/>
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我把这个写进了
functions.php
通过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();
?>
希望这对任何需要它的人都能起到很好的作用,因为这是一项了不起的工作,所以再次感谢斯蒂芬·哈里斯,他不仅帮助了这个问题,而且没有建议我使用他的插件。
最合适的回答,由SO网友:Stephen Harris 整理而成
我会使用WordPress的订阅源。您可以使用创建自己的提要add_feed
. 您指定一个回调,该回调负责显示输出。
创建提要
add_feed(\'my-events\',\'wpse56187_ical_cb\');
wpse56187_ical_cb
负责获取事件(使用
WP_Query
), 循环浏览并打印ICAL字段。
下载ICAL文件
如果您的订阅源是“我的事件”,则可以通过转到下载ICAL文件:
www.yoursite.com?feed=my-events
www.yoursite.com/feed/my-events //If you have pretty permalinks
可以通过以下方式获得到提要的链接
get_feed_link()
. 因此,如果您想在模板中创建指向活动提要的链接:
<a href="<?php echo get_feed_link(\'my-events\'); ?>" > Click here for ical file </a>
示例我省略了以ICAL格式填写事件详细信息的细节(如何填写取决于存储细节的方式)。
下面的类实现了上述方法并创建了一个名为“我的事件”的提要。您需要确保详细信息格式正确且有效(请参阅this site)
请注意,我没有测试以下内容,因此可能存在语法错误
<?php
/**
* Event ICAL feed
*/
class SH_Event_ICAL_Export {
public function load(){
add_feed(\'my-events\', array(__CLASS__,\'export_events\'));
}
/**
* Creates an ICAL file of events in the database
* @param string filename - the name of the file to be created
* @param string filetype - the type of the file (\'text/calendar\')
*/
public function export_events( ){
//Give the ICAL 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 get_bloginfo(\'name\'); ?>//NONSGML Events //EN
CALSCALE:GREGORIAN
X-WR-CALNAME:<?php echo get_bloginfo(\'name\');?> - Events
<?php
// Query for events
$events = WP_Query(array(
\'post_type\'=>\'event\' //Or whatever the name of your post type is
\'posts_per_page\'=>-1 //Get all events
...
));
if( $events->have_posts() ):
while( $events->have_posts() ): $events->the_post();
$uid=\'\'; //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
$start_date=""//event start date
$end_date=""//event end date
$reoccurrence_rule=false//event reoccurrence rule.
$location=\'\'//event location
$organiser=\'\'//event organiser
?>
BEGIN:VEVENT
UID:<?php echo $uid;?>
DTSTAMP:<?php echo $dtstamp;?>
CREATED:<?php echo $created_date;?>
DTSTART:<?php echo $start_date ; ?>
DTEND:<?php echo $end_date; ?>
<?php if ($reoccurrence_rule):?>
RRULE:<?php echo $reoccurrence_rule;?>
<?php endif;?>
LOCATION: <?php echo $location;?>
ORGANIZER: <?php $organiser;?>
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();
SO网友:The J
与Stephen的版本略有不同。我整合了一些东西,改变了其他东西。
代码经过测试,运行良好,因为我正在项目中使用它。
function add_calendar_feed(){
add_feed(\'calendar\', \'export_events\');
// Only uncomment these 2 lines the first time
/*global $wp_rewrite;
$wp_rewrite->flush_rules( false );*/
}
add_action(\'init\', \'add_calendar_feed\');
function export_events(){
// Helper - Get the right time format, use this function or date_i18n(\'Ymd\\THis\\Z\', $yourtimehere, true);
/*function dateToCal($timestamp) {
return date(\'Ymd\\THis\\Z\', $timestamp);
}*/
// Helper - Escapes a string of characters
function escapeString($string) {
return preg_replace(\'/([\\,;])/\',\'\\\\\\$1\', $string);
}
// Helper - Cut it
function shorter_version($string, $lenght) {
if (strlen($string) >= $lenght) {
return substr($string, 0, $lenght);
} else {
return $string;
}
}
// Query the event
$the_event = new WP_Query(array(
\'p\' => $_REQUEST[\'id\'],
\'post_type\' => \'any\',
));
if($the_event->have_posts()) :
while($the_event->have_posts()) : $the_event->the_post();
$timestamp = date_i18n(\'Ymd\\THis\\Z\',time(), true);
$uid = get_the_ID();
$created_date = get_post_time(\'Ymd\\THis\\Z\', true, $uid ); // post creation
$start_date = date_i18n(\'Ymd\\THis\\Z\',time(), true); // EDIT THIS WITH START DATE VALUE
$end_date = date_i18n(\'Ymd\\THis\\Z\',time(), true); // EDIT THIS WITH END DATE VALUE
$deadline = date_i18n(\'Ymd\\THis\\Z\',time()-24, true); // deadline in BEGIN:VALARM
$organiser = get_bloginfo(\'name\'); // YOUR NAME OR SOME VALUE
$address = \'\'; // EDIT THIS WITH SOME VALUE
$url = get_the_permalink();
$summary = get_the_excerpt();
$content = trim(preg_replace(\'/\\s\\s+/\', \' \', get_the_content())); // removes newlines and double spaces
//Give the iCal export a filename
$filename = urlencode( get_the_title().\'-ical-\' . date(\'Y-m-d\') . \'.ics\' );
$eol = "\\r\\n";
//Collect output
ob_start();
// File header
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header(\'Content-type: text/calendar; charset=utf-8\');
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\');?>
BEGIN:VEVENT
CREATED:<?php echo $created_date.$eol;?>
UID:<?php echo $uid.$eol;?>
DTEND;VALUE=DATE:<?php echo $end_date.$eol; ?>
DTSTART;VALUE=DATE:<?php echo $start_date.$eol; ?>
DTSTAMP:<?php echo $timestamp.$eol; ?>
LOCATION:<?php echo escapeString($address).$eol; ?>
DESCRIPTION:<?php echo shorter_version($content,70).$eol; ?>
SUMMARY:<?php echo escapeString(get_the_title()).$eol; ?>
ORGANIZER:<?php echo escapeString($organiser).$eol;?>
URL;VALUE=URI:<?php echo escapeString($url).$eol; ?>
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;VALUE=DATE-TIME:<?php echo $deadline.$eol; ?>
DESCRIPTION:Fine / Scadenza di <?php echo escapeString(get_the_title()); echo $eol; ?>
END:VALARM
END:VEVENT
<?php
endwhile;
?>
END:VCALENDAR
<?php
//Collect output and echo
$eventsical = ob_get_contents();
ob_end_clean();
echo $eventsical;
exit();
endif;
}
?>
然后在主题中,使用以下链接:
<a href="<?php echo get_feed_link(\'calendar\'); ?>?id=<?php echo get_the_ID(); ?>">Get the ical file </a>
制作了一个
GIST 有更多信息。