Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Forked from jo-snips/random-event-widget.php
Created September 20, 2012 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codearachnid/3759022 to your computer and use it in GitHub Desktop.
Save codearachnid/3759022 to your computer and use it in GitHub Desktop.
The Events Calendar: Random Event Widget
<?php
/*
Plugin Name: The Events Calendar: Random Event Widget
Description: This widget will display an upcoming random event (and details) in your sidebar.
Version: 1.0
Author: jonahcoyote
Author URI: http://tri.be?ref=tec-plugin
Text Domain: tribe-widget-random-event
License: GPLv2 or later
*/
/*-----------------------------------------------------------------------------------*/
/* The Events Calendar - Random Event Widget
/*-----------------------------------------------------------------------------------*/
add_action( 'widgets_init', create_function( '', "register_widget( 'Tribe_Widget_Random_Event' );" ) );
class Tribe_Widget_Random_Event extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'widget_random_events',
'description' => __( 'Displays a random event in the next 2 weeks.', 'tribe-widget-random-event' )
);
parent::__construct( 'widget_random_events', __( 'Random Event Widget', 'tribe-widget-random-event' ), $widget_ops );
add_action( 'tribe_widget_random_event_display', array( $this, 'event_display' ), 10, 1 );
}
function form( $instance ) {
// setup defaults or saved values
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __('Random Event','tribe-widget-random-event');
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'tribe-widget-random-event' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function widget( $args, $instance ) {
extract($args);
$title = $instance['title'];
// setup random query args
$args = array(
'post_type' => array(TribeEvents::POSTTYPE), // use post_type IN () to avoid old tribe queries
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_EventStartDate',
'value' => array(
date('Y-m-d H:i:s', strtotime('now')),
date('Y-m-d H:i:s', strtotime('+2 weeks'))),
'compare' => 'BETWEEN',
'type' => 'DATETIME'
)
)
);
$event = new WP_Query( $args );
wp_reset_query();
// if no event is found exit gracefully
if( empty($event->posts) )
return apply_filters('tribe_widget_random_event_none_found', null);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
do_action('tribe_widget_random_event_display', $event);
echo $after_widget;
}
function event_display( $event ){
while ( $event->have_posts() ) : $event->the_post();
?>
<h5 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
<div class="entry-date">
<span class="start"><?php echo tribe_get_start_date(); ?></span>
<?php if(tribe_is_multiday( get_the_ID() ) || tribe_get_all_day( get_the_ID() ) ) : ?>
<span class="divider"> - </span>
<span class="end"><?php echo tribe_get_end_date(); ?></span>
<?php endif; ?>
</div>
<div class="entry-content">
<?php
if (has_excerpt())
the_excerpt();
else
the_content();
?>
<a href="<?php the_permalink(); ?>" class="read-more">View Details &raquo;</a>
</div>
<?php
endwhile;
}
}
@codearachnid
Copy link
Author

To find events that start or end within 2 weeks just change the args array or add a filter to tribe_widget_random_event_none_found

$args = array(
        'post_type'      => array(TribeEvents::POSTTYPE), // use post_type IN () to avoid old tribe queries
        'posts_per_page' => 1,
        'orderby'        => 'rand',
        'post_status'    => 'publish',
        'meta_query'     => array(
          'relation' => 'OR',
          array(
            'key'     => '_EventStartDate',
            'value'   => array(
              date('Y-m-d H:i:s', strtotime('now')),
              date('Y-m-d H:i:s', strtotime('+2 weeks'))),
            'compare' => 'BETWEEN',
            'type'    => 'DATETIME'
          ),
          array(
            'key'     => '_EventEndDate',
            'value'   => array(
              date('Y-m-d H:i:s', strtotime('now')),
              date('Y-m-d H:i:s', strtotime('+2 weeks'))),
            'compare' => 'BETWEEN',
            'type'    => 'DATETIME'
          )
        )
      );

@themenfreund
Copy link

the directory where I save the file with the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment