Created
July 5, 2013 23:44
-
-
Save ammist/5937893 to your computer and use it in GitHub Desktop.
This is a simple way to add event meta field on WordPress posts. You could use this for a custom post type, but this is some code I wrote in 2010 to extend Thesis with event information. It doesn't support recurring events, or a calendar view, but it's pretty darn handy for something really simple.
This file contains 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 | |
/** | |
* TL EVENTS | |
* | |
* Functionality for adding time, date for events, displaying them by week, etc. | |
* This assumes your events are posts in the "Events" category, but could easily be | |
* extended to other stuff. | |
*/ | |
/* --------------- EVENT FUNCTIONS - EDITOR ----------------- */ | |
function tl_editor_customization() { | |
// add our custom styles | |
wp_enqueue_style( 'tl_custom_css', get_stylesheet_uri() . '/css/admin.css'); | |
add_meta_box('tl_event_meta', 'Event Information','tl_event_meta_box', 'post', 'side', 'low'); | |
} | |
function tl_event_meta_box(){ | |
global $post; | |
$event_fields = tl_event_fields(); | |
echo '<p>To add an event to your post, check the "Events" category, then enter your event start and end dates here. <a href="/help/">Read more about Events.</a></p>'; | |
$start_date = get_post_meta($post->ID, 'event_start_date', TRUE); | |
if ($start_date){ | |
$start_date_fields = explode("/", $start_date); | |
$start_month = isset($start_date_fields[1]) ? $start_date_fields[1] : 0; | |
$start_year = isset($start_date_fields[0]) ? $start_date_fields[0] : ""; | |
$start_day = isset($start_date_fields[2]) ? $start_date_fields[2] : ""; | |
} else { | |
$start_month=0; | |
$start_year=2010; | |
$start_day=0; | |
} | |
$start_time = get_post_meta($post->ID, 'event_start_time', TRUE); | |
if ($start_time){ | |
$start_date_fields = explode(":", $start_time); | |
$start_hour = isset($start_date_fields[0]) ? $start_date_fields[0] : ""; | |
$start_minute = isset($start_date_fields[1]) ? $start_date_fields[1] : ""; | |
if ($start_hour && !$start_minute){ | |
$start_minute="00"; | |
} | |
$start_ampm = $start_hour > 11 ? "PM" : "AM"; | |
if ($start_hour == 0){ | |
$start_hour = 12; | |
} elseif ($start_hour > 12){ | |
$start_hour -= 12; | |
} | |
} else { | |
$start_hour = 0; | |
$start_minute = -1; | |
$start_ampm = "00"; | |
} | |
$end_date = get_post_meta($post->ID, 'event_end_date', TRUE); | |
if ($end_date){ | |
$end_date_fields = explode("/", $end_date); | |
$end_month = isset($end_date_fields[1]) ? $end_date_fields[1] : 0; | |
$end_year = isset($end_date_fields[0]) ? $end_date_fields[0] : ""; | |
$end_day = isset($end_date_fields[2]) ? $end_date_fields[2] : ""; | |
} else { | |
$end_month=0; | |
$end_year="2010"; | |
$end_day="00"; | |
} | |
$end_time = get_post_meta($post->ID, 'event_end_time', TRUE); | |
if ($end_time){ | |
$end_date_fields = explode(":", $end_time); | |
$end_hour = isset($end_date_fields[0]) ? $end_date_fields[0] : ""; | |
$end_minute = isset($end_date_fields[1]) ? $end_date_fields[1] : ""; | |
if ($end_hour && !$end_minute){ | |
$end_minute="00"; | |
} | |
$end_ampm = $end_hour > 11 ? "PM" : "AM"; | |
if ($end_hour == 0){ | |
$end_hour = 12; | |
} elseif ($end_hour > 12){ | |
$end_hour -= 12; | |
} | |
} else { | |
$end_hour = 0; | |
$end_minute =-1; | |
$end_ampm ="00"; | |
} | |
$event_location = get_post_meta($post->ID, 'event_location', TRUE); | |
$minute_increments = array("00", "15", "30", "45"); | |
?> | |
<table class="timestamp-wrap"> | |
<tr><th colspan="2"><b>Start Info:</b></th></tr> | |
<tr><td>Date: </td><td><select id="es_month" name="es_month" tabindex="140"> | |
<option value="00" <?php if($start_month == 0) echo 'selected="selected"'; ?>>Month</option> | |
<option value="01" <?php if($start_month == 1) echo 'selected="selected"'; ?>>Jan</option> | |
<option value="02" <?php if($start_month == 2) echo 'selected="selected"'; ?>>Feb</option> | |
<option value="03" <?php if($start_month == 3) echo 'selected="selected"'; ?>>Mar</option> | |
<option value="04" <?php if($start_month == 4) echo 'selected="selected"'; ?>>Apr</option> | |
<option value="05" <?php if($start_month == 5) echo 'selected="selected"'; ?>>May</option> | |
<option value="06" <?php if($start_month == 6) echo 'selected="selected"'; ?>>Jun</option> | |
<option value="07" <?php if($start_month == 7) echo 'selected="selected"'; ?>>Jul</option> | |
<option value="08" <?php if($start_month == 8) echo 'selected="selected"'; ?>>Aug</option> | |
<option value="09" <?php if($start_month == 9) echo 'selected="selected"'; ?>>Sep</option> | |
<option value="10" <?php if($start_month == 10) echo 'selected="selected"'; ?>>Oct</option> | |
<option value="11" <?php if($start_month == 11) echo 'selected="selected"'; ?>>Nov</option> | |
<option value="12" <?php if($start_month == 12) echo 'selected="selected"'; ?>>Dec</option> | |
</select><select id="es_day" name="es_day" tabindex="140"> | |
<option value="00" <?php if($start_day == 0) echo 'selected="selected"'; ?>>Day</option> | |
<? | |
for ($i=1; $i<32; $i++){ | |
$dayval = sprintf("%02d", $i); | |
?> | |
<option value="<?php echo $dayval; ?>" <?php if($start_day == $dayval) echo 'selected="selected"'; ?>><?php echo $dayval; ?></option> | |
<? | |
} | |
?> | |
</select>,<select id="es_year" name="es_year"> | |
<option value="00" <?php if($start_year == 0) echo 'selected="selected"'; ?>>Year</option> | |
<? | |
for ($i=2010; $i<2013; $i++){ | |
?> | |
<option value="<?php echo $i; ?>" <?php if($start_year == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> | |
<? | |
} | |
?> | |
</select></td></tr> | |
<tr><td>Time: </td><td><select id="es_hour" name="es_hour"> | |
<option value="00" <?php if($start_hour == 0) echo 'selected="selected"'; ?>>Hour</option> | |
<? | |
for ($i=1; $i<13; $i++){ | |
?> | |
<option value="<?php echo $i; ?>" <?php if($start_hour == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> | |
<? | |
} | |
?> | |
</select>:<select id="es_minute" name="es_minute"> | |
<option value="-1" <?php if($start_minute == -1) echo 'selected="selected"'; ?>>Min</option> | |
<? | |
foreach ($minute_increments as $i){ | |
?> | |
<option value="<?php echo $i; ?>" <?php if($start_minute == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> | |
<? | |
} | |
?> | |
</select><select id="es_ampm" name="es_ampm" > | |
<option value="AM" <?php if ($start_ampm != "PM") echo 'selected="selected"'; ?>>AM</option> | |
<option value="PM" <?php if ($start_ampm == "PM") echo 'selected="selected"'; ?>>PM</option> | |
</select><br/></td></tr> | |
<tr><th colspan=2><b>End Info:</b></th></tr> | |
<tr><td>Date:</td><td><select id="ed_month" name="ed_month" tabindex="140"> | |
<option value="00" <?php if($end_month == 0) echo 'selected="selected"'; ?>>Month</option> | |
<option value="01" <?php if($end_month == 1) echo 'selected="selected"'; ?>>Jan</option> | |
<option value="02" <?php if($end_month == 2) echo 'selected="selected"'; ?>>Feb</option> | |
<option value="03" <?php if($end_month == 3) echo 'selected="selected"'; ?>>Mar</option> | |
<option value="04" <?php if($end_month == 4) echo 'selected="selected"'; ?>>Apr</option> | |
<option value="05" <?php if($end_month == 5) echo 'selected="selected"'; ?>>May</option> | |
<option value="06" <?php if($end_month == 6) echo 'selected="selected"'; ?>>Jun</option> | |
<option value="07" <?php if($end_month == 7) echo 'selected="selected"'; ?>>Jul</option> | |
<option value="08" <?php if($end_month == 8) echo 'selected="selected"'; ?>>Aug</option> | |
<option value="09" <?php if($end_month == 9) echo 'selected="selected"'; ?>>Sep</option> | |
<option value="10" <?php if($end_month == 10) echo 'selected="selected"'; ?>>Oct</option> | |
<option value="11" <?php if($end_month == 11) echo 'selected="selected"'; ?>>Nov</option> | |
<option value="12" <?php if($end_month == 12) echo 'selected="selected"'; ?>>Dec</option> | |
</select><select id="ed_day" name="ed_day" tabindex="140"> | |
<option value="00" <?php if($end_day == 0) echo 'selected="selected"'; ?>>Day</option> | |
<? | |
for ($i=1; $i<32; $i++){ | |
$dayval = sprintf("%02d", $i); | |
?> | |
<option value="<?php echo $dayval; ?>" <?php if($end_day == $dayval) echo 'selected="selected"'; ?>><?php echo $dayval; ?></option> | |
<? | |
} | |
?> | |
</select>,<select id="ed_year" name="ed_year"> | |
<option value="00" <?php if($end_year == 0) echo 'selected="selected"'; ?>>Year</option> | |
<? | |
for ($i=2010; $i<2013; $i++){ | |
?> | |
<option value="<?php echo $i; ?>" <?php if($end_year == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> | |
<? | |
} | |
?> | |
</select></td></tr> | |
<tr><td>Time:</td><td><select id="ed_hour" name="ed_hour"> | |
<option value="00" <?php if($end_hour == 0) echo 'selected="selected"'; ?>>Hour</option> | |
<? | |
for ($i=1; $i<13; $i++){ | |
?> | |
<option value="<?php echo $i; ?>" <?php if($end_hour == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> | |
<? | |
} | |
?> | |
</select>:<select id="ed_minute" name="ed_minute"> | |
<option value="-1" <?php if($end_minute == -1) echo 'selected="selected"'; ?>>Min</option> | |
<? | |
foreach ($minute_increments as $i){ | |
?> | |
<option value="<?php echo $i; ?>" <?php if($end_minute == $i) echo 'selected="selected"'; ?>><?php echo $i; ?></option> | |
<? | |
} | |
?> | |
</select><select id="ed_ampm" name="ed_ampm" > | |
<option value="AM" <?php if ($end_ampm != "PM") echo 'selected="selected"'; ?>>AM</option> | |
<option value="PM" <?php if ($end_ampm == "PM") echo 'selected="selected"'; ?>>PM</option> | |
</select></td></tr> | |
<tr><td>Location: </td><td><input type="text" name='event_location' id="event_locatione" | |
value="<?php echo $event_location ;?>" /> </td></tr></table> | |
<input type="hidden" name='event_noncename' id="event_nonce" | |
value="<?php wp_create_nonce(plugin_basename(__FILE__));?>" /> | |
<? | |
} | |
function tl_event_fields(){ | |
$fields = array( | |
'start_date' => array( | |
'name' =>'event_start_date', | |
'label' => 'Start Date', | |
'default' => '', | |
'value' => '' | |
), | |
'end_date' => array( | |
'name' => 'event_start_time', | |
'label' => 'Start Time', | |
'default' => '', | |
'value' => '' | |
), | |
'start_time' => array( | |
'name' =>'event_end_date', | |
'label' => 'End Date', | |
'default' => '', | |
'value' => '' | |
), | |
'end_time' => array ( | |
'name' => 'event_end_time', | |
'label' => 'End Time', | |
'default' => '', | |
'value' => '' | |
), | |
'location' => array ( | |
'name' => 'event_location', | |
'label' => 'Location', | |
'default' => '', | |
'value' => '' | |
) | |
); | |
return $fields; | |
} | |
/* Save the data stored in your custom fields */ | |
function tl_save_postdata($post_id){ | |
$start_time = get_post_meta($post_id, 'event_start_time', TRUE); | |
$start_date = get_post_meta($post_id, 'event_start_date', TRUE); | |
$end_date = get_post_meta($post_id, 'event_end_date', TRUE); | |
$end_time = get_post_meta($post_id, 'event_end_time', TRUE); | |
$event_location = get_post_meta($post_id, 'event_location', TRUE); | |
// get each set of values and make sure they are valid before we replace them. | |
$new_start_fields[0] = $_POST['es_year']; | |
$new_start_fields[1] = $_POST['es_month']; | |
$new_start_fields[2] = $_POST['es_day']; | |
$date_check = checkdate($new_start_fields[1], intval($new_start_fields[2]), intval($new_start_fields[0])); | |
if ($date_check){ | |
$new_start_date = implode ("/", $new_start_fields); | |
update_post_meta($post_id, 'event_start_date', $new_start_date); | |
} else { | |
if ($start_date != ""){ | |
delete_post_meta($post_id, 'event_start_date'); | |
} | |
} | |
$new_start_fields[0] = $_POST['es_hour']; | |
$new_start_fields[1] = $_POST['es_minute']; | |
$new_start_fields[2] = $_POST['es_ampm']; | |
if (0 < $new_start_fields[0] && $new_start_fields[0] < 10){ | |
$new_start_fields[0] = "0" . intval($new_start_fields[0]); | |
} | |
if (($new_start_fields[0] > 0) && $new_start_fields[1] == -1){ | |
$new_start_fields[1] = "00"; | |
} | |
// default to morning events | |
if ($new_start_fields[0] && !$new_start_fields[2]){ | |
$new_start_fields[2] = "AM"; | |
} | |
if (($new_start_fields[0] > 0) && $new_start_fields[1] && $new_start_fields[2]){ | |
$new_start_time = tl_time_store($new_start_fields); | |
update_post_meta($post_id, 'event_start_time', $new_start_time); | |
} else { | |
if ($start_time != ""){ | |
delete_post_meta($post_id, 'event_start_time'); | |
} | |
} | |
// get each set of values and make sure they are valid before we replace them. | |
$new_end_fields[0] = $_POST['ed_year']; | |
$new_end_fields[1] = $_POST['ed_month']; | |
$new_end_fields[2] = $_POST['ed_day']; | |
$date_check = checkdate($new_end_fields[1], intval($new_end_fields[2]), intval($new_end_fields[0])); | |
if ($date_check){ | |
$new_end_date = implode ("/", $new_end_fields); | |
update_post_meta($post_id, 'event_end_date', $new_end_date); | |
} else { | |
if ($end_date != ""){ | |
delete_post_meta($post_id, 'event_end_date'); | |
} | |
} | |
$new_end_fields[0] = $_POST['ed_hour']; | |
$new_end_fields[1] = $_POST['ed_minute']; | |
$new_end_fields[2] = $_POST['ed_ampm']; | |
// add leading zeros to hours | |
if (0 < $new_end_fields[0] && $new_end_fields[0] < 10){ | |
$new_end_fields[0] = "0" . intval($new_end_fields[0]); | |
} | |
if (($new_end_fields[0] > 1) && $new_end_fields[1] == -1){ | |
$new_end_fields[1] = "00"; | |
} | |
if ($new_end_fields[0] && !$new_end_fields[2]){ | |
$new_end_fields[2] = "AM"; | |
} | |
if (($new_end_fields[0] > 0) && $new_end_fields[1] && $new_end_fields[2]){ | |
$new_end_time = tl_time_store($new_end_fields); | |
update_post_meta($post_id, 'event_end_time', $new_end_time); | |
} else { | |
if ($end_time != ""){ | |
delete_post_meta($post_id, 'event_end_time'); | |
} | |
} | |
$new_location = $_POST['event_location']; | |
if ($new_location){ | |
update_post_meta($post_id, 'event_location', $new_location); | |
} else { | |
delete_post_meta($post_id, 'event_location'); | |
} | |
} | |
/* --------------------- tl_week_events ---------------------*/ | |
// returns an array of this week's events, grouped by day. | |
function tl_week_events($sitearea = 1, $fullcontent=false){ | |
global $wpdb; | |
$cur_site = $wpdb->blogid; | |
if ($sitearea == tl_HOMESITE || $sitearea == tl_PR){ | |
switch_to_blog(tl_ANNOUNCEMENT_BLOG); | |
} | |
// We're going to get the data from the announcements blog, which | |
// conveniently provides us with links back to the original page. | |
/* note about weekday: PHP assumes the week starts on Sunday, but for our | |
purposes we want a Monday start. | |
*/ | |
$weekday = date('N'); // Current day of the week, assuming a Sunday start. | |
$today = date("o/m/d"); | |
$weekstart = date("o/m/d", mktime(0, 0, 0, date('m'), date('d')-($weekday - 1), date('o'))); | |
$weekend = date("o/m/d", mktime(0,0,0, date('m'), date('d')+(7 - $weekday), date('o'))); | |
//echo ("Weekday: $weekday | Week Starts on: $weekstart | Week ends on $weekend <br/><br/>"); | |
$week_events = array( | |
'1' => array('label' => 'Monday', | |
'date' =>date("M d, o", mktime(0, 0, 0, date('m'), date('d') + (1 - $weekday), date('o'))), | |
'events' => array() | |
), | |
'2' => array('label' => 'Tuesday', | |
'date' => date("M d, o", mktime(0, 0, 0, date('m'), date('d')+ (2 - $weekday))), | |
'events' => array() | |
), | |
'3' => array('label' => 'Wednesday', | |
'date' => date("M d, o", mktime(0, 0, 0, date('m'), date('d')+ (3 - $weekday))), | |
'events' => array() | |
), | |
'4' => array('label' => 'Thursday', | |
'date' => date("M d, o", mktime(0, 0, 0, date('m'), date('d')+ (4 - $weekday))), | |
'events' => array() | |
), | |
'5' => array('label' => 'Friday', | |
'date' => date("M d, o", mktime(0, 0, 0, date('m'), date('d')+ (5 - $weekday))), | |
'events' => array() | |
), | |
'6' => array('label' => 'Weekend', | |
'date' => date("M d, o", mktime(0, 0, 0, date('m'), date('d')+ (6 - $weekday))) . " - " . date("M d, o", mktime(0, 0, 0, date('m'), date('d')+ (7 - $weekday))), | |
'events' => array() | |
) | |
); // 6 = "weekend" | |
// get the events that start and end this week | |
$postids = $wpdb->get_col($wpdb->prepare(" | |
SELECT DISTINCT post_id | |
FROM $wpdb->postmeta | |
WHERE (meta_key = 'event_start_date' OR meta_key='event_end_date') | |
AND (meta_value >= %s AND meta_value <= %s) order by meta_value", $weekstart, $weekend)); | |
//echo serialize($postids); | |
if ($postids) { | |
$args=array( | |
'post__in' => $postids, | |
'category_name' => 'events', | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'caller_get_posts'=> 1, | |
'meta_key' => 'event_start_time', | |
'orderby' => 'meta_value', | |
'order'=> 'ASC' | |
); | |
$my_query = null; | |
$my_query = new WP_Query($args); | |
global $post; | |
while ($my_query->have_posts()) : | |
// We will use arrays of arrays to list all the events that | |
// take place during the week. Indexing will be based on the | |
// value of "weekday" | |
// put the appropriate posts in the day's list, based on start and end date | |
$my_query->the_post(); | |
$start_date = get_post_meta($post->ID, 'event_start_date', TRUE); | |
//$start_date = isset($start_date) ? $start_date) : ""; | |
$start_time = get_post_meta($post->ID, 'event_start_time', TRUE); | |
//$start_time = isset($start_time) ? strtotime($start_time) : ""; | |
$end_date = get_post_meta($post->ID, 'event_end_date', TRUE); | |
$end_date = isset($end_date) ? $end_date : $start_date; | |
$end_time = get_post_meta($post->ID, 'event_end_time', TRUE); | |
//$end_time = isset($end_time) ? strtotime($end_time) : ""; | |
$event = array('postid' => $post->ID, | |
'start_date' => $start_date, | |
'start_time' => tl_time_display($start_time), | |
'end_date' => $end_date, | |
'end_time' => tl_time_display($end_time), | |
'title' => $post->post_title, | |
'url' => get_permalink($post->ID), | |
'location' => get_post_meta($post->ID, 'event_location', TRUE), ); | |
if ($fullcontent){ | |
$event['content'] = apply_filters('the_content', $post->post_content); | |
} | |
if ($event['start_date'] && ($event['start_date'] <= $weekstart)){ | |
$firstday = 1; | |
} else { | |
$firstday = date("N", strtotime($event['start_date'])); | |
} | |
// If the event spans multiple days, it should appear for all of them. | |
$lastday = $firstday; | |
if ($event['end_date'] && ($event['end_date'] >= $weekend)){ | |
$lastday = 6; | |
} elseif ($event['end_date']){ | |
$lastday = date("N", strtotime($event['end_date'])); | |
} | |
if ($firstday == 7) $firstday = 6; // we don't care about sunday. | |
if ($lastday == 7) $lastday = 6; | |
for ($i = $firstday; $i <= $lastday; $i++){ | |
$week_events[$i]['events'][] = $event; | |
} | |
endwhile; | |
} | |
wp_reset_query(); // Restore global post data stomped by the_post(). | |
if ($wpdb->blog_id != $cur_site) { | |
switch_to_blog($cur_site); | |
} | |
return $week_events; | |
} | |
/* --------------- EVENT FUNCTIONS - Display Information */ | |
// Get the events happening this week. Passing a blog ID will | |
// get the events for that blog. The default is all relevant | |
// site blogs. | |
// format the time for our desired display style | |
function tl_time_display($start_time){ | |
if ($start_time){ | |
$tmp_time = explode(":", $start_time); | |
$hour = isset($tmp_time[0]) ? $tmp_time[0] : -1; | |
$minute = (isset($tmp_time[1]) && $hour >= 0) ? $tmp_time[1] : -1; | |
$ampm = $hour > 11 ? "PM" : "AM"; | |
if ($hour == 0){ | |
$hour = 12; | |
} elseif ($hour > 12){ | |
$hour -= 12; | |
} | |
if ($hour < 1){ | |
$start_time = "All Day"; | |
} else { | |
$start_time = $hour . ':' . $minute . ' ' . $ampm; | |
} | |
} else { | |
$start_time = "All Day"; | |
} | |
return $start_time; | |
} | |
// Return a versio of the time that will be stored in the DB in 24-hour time format. | |
function tl_time_store($time_array){ | |
if ($time_array[2] == "PM"){ | |
$time_array[0] += 12; | |
if ($time_array[0] == 24) $time_array[0] = 12; | |
} elseif ($time_array[0] == 12 && $time_array[2] == "AM") | |
$time_array[0] = "00"; | |
return $time_array[0] . ":" . $time_array[1]; | |
} | |
// Display an event's information using hooks. You could just call this | |
// function in your theme. | |
add_action('thesis_hook_after_headline', 'tl_display_event_info'); | |
add_action('genesis_post_content', 'tl_display_event_info'); | |
function tl_display_event_info(){ | |
global $post; | |
if (in_category('events')){ | |
$start_date = get_post_meta($post->ID, 'event_start_date', TRUE); | |
if ($start_date){ | |
$tmp_time = strtotime($start_date); | |
if ($tmp_time){ | |
$start_date = date("l, M d Y", $tmp_time); | |
} | |
} | |
$start_time = get_post_meta($post->ID, 'event_start_time', TRUE); | |
$start_time = tl_time_display($start_time); | |
$end_date = get_post_meta($post->ID, 'event_end_date', TRUE); | |
if ($end_date){ | |
$tmp_time = strtotime($end_date); | |
if ($tmp_time){ | |
$end_date = date("l, M d Y", $tmp_time); | |
} | |
} | |
$end_time = get_post_meta($post->ID, 'event_end_time', TRUE); | |
$end_time = tl_time_display($end_time); | |
if ($end_date == $start_date){ | |
$end_date = ""; | |
} | |
if ($start_time == 'All Day' || $end_time == "All Day"){ | |
$end_time = ""; | |
} | |
$location = get_post_meta($post->ID, 'event_location', TRUE); | |
?> | |
<div class="event_info vevent"> | |
<p class="event_date"><span class="dtstart"><?php echo $start_date;?>, <?php echo $start_time;?></span> | |
<?php if ($end_date || $end_time) { | |
?> | |
— <span class="dtend"><?php echo $end_date;?> | |
<?php if ($end_time){ if ($end_date) {echo ", " . $end_time; } else {echo $end_time;}} ?></span> | |
<?php | |
} | |
?> | |
</p> | |
<?php | |
if ($location){ | |
?> | |
<p class="event_location location"><?php echo $location; ?></p> | |
<?php | |
} | |
?> | |
</div> | |
<? | |
} | |
} | |
/* --------------- THIS WEEK WIDGET - Display event information in a widget ---- */ | |
class tl_This_Week extends WP_Widget { | |
function tl_This_Week() { | |
$widget_ops = array('classname' => 'tl_this_week', 'description' => __( 'Shows events happening this week.') ); | |
$this->WP_Widget('jgi-this-week', __('This Week\'s Events'), $widget_ops); | |
} | |
// displays the appropriate list of events for a given site area | |
function widget( $args, $instance ) { | |
extract( $args ); | |
global $wpdb; | |
$noevents_text = '<ul class="week_events"><li>No events are currently scheduled for this week.</li></ul>'; | |
$noevents = true; | |
$week_events = tl_week_events($wpdb->blogid); | |
$out = ""; | |
if ($week_events){ | |
$out = "<ul class='week_events hcalendar'>"; | |
foreach ($week_events as $day_events){ | |
if (isset($day_events['events'][0])){ | |
$out .= "<li class='day-heading'>" . $day_events['label'] ."\n<ul>"; | |
foreach ($day_events['events'] as $event){ | |
$start_time_class = $event['start_time'] == 'All Day' ? "allday" : "event_start_time"; | |
$out .= '<li><span class="' . $start_time_class . '">'. $event['start_time'] . '</span> <a class="event_name" href="' . $event['url'] . '">' . $event['title'] . "</a></li>"; | |
} | |
$noevents = false; | |
$out.= "</ul></li>"; //close day's events | |
} | |
} | |
$out .= "</ul>"; //close main container | |
//echo serialize($week_events); | |
} | |
if ($noevents || $out == ""){ | |
$out = $noevents_text; | |
} | |
if ($wpdb->blogid == tl_HOMESITE || $wpdb->blogid == tl_PR){ | |
$title = "This Week's Events"; | |
} else { | |
$title = "This Week's " . get_bloginfo($wpdb->blogid, "blogname") . " Events"; | |
} | |
echo $before_widget; | |
echo $before_title . $title . $after_title; | |
echo $out; | |
echo '<p class="more_link"><a href="/events">All Events</a></p>'; | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
return $instance; | |
} | |
function form ( $instance ){ | |
?> | |
<p>This widget has no options at this time</p> | |
<? | |
} | |
} // tl_This_Week class | |
add_action('widgets_init', create_function('', 'return register_widget("tl_This_Week");')); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment