Event Calendar Widget

<?php
/**
* Events Calendar Widget
*
* @link http://codex.wordpress.org/Widgets_API#Developing_Widgets
*
* @package BE_Genesis_Child
* @author Bill Erickson <[email protected]>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
class BE_Events_Calendar_Widget extends WP_Widget {
/**
* Constructor
*
* @return void
**/
function BE_Events_Calendar_Widget() {
$widget_ops = array( 'classname' => 'widget_events_calendar', 'description' => '' );
$this->WP_Widget( 'events-calendar-widget', 'Events Calendar', $widget_ops );
}
/**
* Outputs the HTML for this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void Echoes it's output
**/
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
// If they've selected a date, then $current = that.
// If not, then $current = today
if( isset( $_GET['event-date'] ) )
$current = esc_attr( $_GET['event-date'] );
else
$current = date( 'M-d-Y' );
// Build Calendar Here.
$date = isset( $_GET['event-date'] ) ? esc_attr( $_GET['event-date'] ) : '';
if( empty( $date ) ) // if no date selected, use today
$date = date( 'M-d-Y' );
global $post;
if( is_singular() ) $date = date( 'M-d-Y', get_post_meta( $post->ID, 'be_event_start_date_unix', true ) );
$month = date( 'F', strtotime( $date ) );
$month_numeric = date( 'm', strtotime( $date ) );
$month_short = date( 'M', strtotime( $date ) );
$year = date( 'Y', strtotime( $date ) );
$prev_month_date = $this->build_prev_month_link( $date );
$next_month_date = $this->build_next_month_link( $date );
$first_day = mktime(0,0,0,$month_numeric, 1, $year);
$day_of_week = date('D', $first_day);
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
$days_in_month = cal_days_in_month(0, $month_numeric, $year);
?>
<div class="calendar-month">
<?php
echo '<a href="' . add_query_arg( 'event-date', $prev_month_date, get_post_type_archive_link( 'events' ) ) . '" class="prev">&laquo;</a>';
echo '<a href="' . add_query_arg( 'event-date', $next_month_date, get_post_type_archive_link( 'events' ) ) . '" class="next">&raquo;</a>';
echo '<strong>' . $month . '</strong>';
?>
</div>
<div class="calendar-day-names">
<span>S</span><span>M</span><span>T</span><span>W</span><span>T</span><span>F</span><span>S</span>
</div>
<div class="calendar-days">
<?php
$day_count = 1;
while ( $blank>0 ) {
echo '<span>&nbsp;</span>';
$blank--;
$day_count++;
}
$day_num = 1;
while ( $day_num <= $days_in_month ) {
$day_num_leading_zero = strlen((string)$day_num) == 1 ? ('0'.$day_num) : $day_num;
echo $this->build_event_link( $month_short . '-' . $day_num_leading_zero . '-' . $year );
$day_num++;
$day_count++;
if ($day_count > 7) {
echo '<div class="cl"></div>';
$day_count = 1;
}
}
while ( $day_count > 1 && $day_count <=7 ) {
$day_count++;
}
?>
</div><!-- /calendar-days -->
<?php
echo $after_widget;
}
function build_event_link( $date ) {
$current = isset( $_GET['event-date'] ) ? esc_attr( $_GET['event-date'] ) : date( 'M-d-Y' );
global $post;
if( is_singular() ) $current = date( 'M-d-Y', get_post_meta( $post->ID, 'be_event_start_date_unix', true ) );
$day = date( 'j', strtotime( $date ) );
$classes = ( $date == $current && ( is_post_type_archive( 'events' ) || is_singular() ) ) ? ' class="active"' : '';
$output = '<a href="' . add_query_arg( 'event-date', $date, get_post_type_archive_link( 'events' ) ) . '"' . $classes . '>' . $day . '</a> ';
return $output;
}
function build_prev_month_link( $date ) {
$prev_month = date( 'M', strtotime( '-1 Months', strtotime($date) ) );
$prev_month_year = date( 'Y', strtotime( '-1 Months', strtotime($date) ) );
return $prev_month . '-01-' . $prev_month_year;
}
function build_next_month_link( $date ) {
$next_month = date( 'M', strtotime( '+1 Months', strtotime($date) ) );
$next_month_year = date( 'Y', strtotime( '+1 Months', strtotime($date) ) );
return $next_month . '-01-' . $next_month_year;
}
}
add_action( 'widgets_init', 'be_register_calendar_widget' );
/**
* Register Calendar Widget
*
*/
function be_register_calendar_widget() {
register_widget('BE_Events_Calendar_Widget');
}

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk