下面是一个最简单的工作示例。。。(注意:这是未测试的代码。)
add_action(\'init\', \'custom_rsvp_handler\');
function custom_rsvp_handler() {
// for singular posts only
// (could also check for event post type here)
if (!is_singular()) {return;}
// set cookie expiry length (eg. 28 days)
$cookielength = time()+(60*60*24*28);
// set a cookie key based on post ID
global $post; $cookiekey = \'rsvp-\'.$post->ID;
// set global to access RSVP state in shortcode
global $rsvp;
// check if already visited (RSVP cookie set)
if (isset($_COOKIE[$cookiekey])) {$rsvp = $_COOKIE[$cookiekey];}
// check for RSVP querystring (clicked email link with ?rsvp=1)
// (or clicked attending after clicking not attending)
if (isset($_GET[\'rsvp\'])) {
$ip = $_SERVER[\'REMOTE_ADDR\'];
// check for email click
if ($_GET[\'rsvp\'] == \'1\') {
$rsvp = \'1\';
if (!isset($_COOKIE[$cookiekey])) {
// assume this is a new click and set cookie
setcookie($cookiekey, \'1\', $cookielength);
// record the IP to the post meta
$ips = get_post_meta($post->ID, \'rsvps\');
if ( (!is_array($ips)) || (!in_array($ip, $ips)) ) {
$ips[] = $ip; update_post_meta($post->ID, \'rsvps\', $ips);
update_post_meta($post->ID, \'rsvp-count\', count($ips));
}
}
} elseif ($_GET[\'rsvp\'] == \'0\') {
// user clicked not attending button
$rsvp = \'0\'; setcookie($cookiekey, \'0\', $cookielength);
// remove IP from attending list
$ips = get_post_meta($post->ID, \'rsvps\');
if ( (is_array($ips)) || (in_array($ip, $ips)) ) {
$index = array_search($ip, $ips); unset($ips[$index]);
update_post_meta($post->ID, \'rsvps\', $ips);
update_post_meta($post->ID, \'rsvp-count\', count($ips));
}
}
}
}
}
// shortcode for a non-attending button
add_shortcode(\'rsvp-button\', \'custom_rsvp_button\');
function custom_rsvp_button() {
global $rsvp;
if (!isset($rsvp)) {return \'\';}
if ($rsvp == \'1\') {$action = \'I Cannot Attend\'; $switch = \'0\';}
if ($rsvp == \'0\') {$action = \'I Can Attend\'; $switch = \'1\';}
$button = \'<form method="get" action="\'.$_SERVER[\'PHP_SELF\'].\'">\';
$button .= \'<input type="hidden" name="rsvp" value="\'.$switch.\'">\';
$button .= \'<input type="submit" value="\'.$action.\'">\';
$button .= \'</form>\';
return $button;
}
add_shortcode(\'rsvp-count\', \'custom_rsvp_count\');
function custom_rsvp_count() {
global $rsvp, $post;
if (!isset($rsvp)) {return;}
$attendees = get_post_meta($post->ID, \'rsvp-count\');
if ($attendees) {return $attendees." Attending";}
return \'\';
}
因此,电子邮件链接RSVP URL如下所示:
http://example.com/event-post/?rvsp=1这将把IP列表保存到事件帖子的meta中,并为访问者设置一个cookie作为参与者,这将(大致)防止重复记录。
然后,您可以将快捷码[rvsp按钮]添加到活动帖子内容中,以便页面访问者可以在访问页面后单击“我无法参加”。或“我可以参加”,如果他们已经点击他们不能参加。(未通过电子邮件链接到达的访问者将看不到该按钮。)
更新:为输出rsvp参与计数添加了post meta和shortcode[rsvp计数]。