Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@megmorsie
Last active July 21, 2019 15:00
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 megmorsie/f3f3b51adfa677dc243a147a26090740 to your computer and use it in GitHub Desktop.
Save megmorsie/f3f3b51adfa677dc243a147a26090740 to your computer and use it in GitHub Desktop.
Quick Plugin Made for Cleveland GiveCamp 2018 & Updated for 2019 | Usage: [events] | Requires The Events Calendar to be installed and activated.
<?php
/*
* Plugin Name: TCBA Events Shortcode
* Plugin URI: https://gist.github.com/megmorsie/f3f3b51adfa677dc243a147a26090740
* Description: Sets up a shortcode [events] to be used to display an unordered list of upcoming events (set to 5).
* Version: 1.0
* Author: Cle GiveCamp
* Author URI: http://clevelandgivecamp.org
* License: GPL2
* Text Domain: cle_givecamp
*/
// Supporting Documentation for This Plugin's Functionality:
// https://support.theeventscalendar.com/666307-Using-tribe_get_events
add_shortcode('events', 'cgc_return_event_list_shortcode');
function cgc_return_event_list_shortcode() {
if ( function_exists('tribe_get_events') ) {
// Patch found here: https://wordpress.org/support/topic/tribe_get_events-stopped-returning-eventstartdate-and-eventenddate-by-default/
add_filter( 'tribe_get_events', 'mdw_add_back_EventEndDate', 5, 1 );
function mdw_add_back_EventEndDate($events) {
$updated_events = array();
foreach($events as $event):
$event->EventStartDate = tribe_get_start_date( $event, false, 'Y-m-j g:i a');
$event->EventEndDate = tribe_get_end_date( $event, false, 'Y-m-j g:i a');
$updated_events[]=$event;
endforeach;
return $updated_events;
}
$event_list = 'No upcoming events available.';
$five_events = tribe_get_events( array(
'posts_per_page' => 5,
'start_date' => 'now'
) );
if ( $five_events ) {
$event_list = '<ul>';
foreach ( $five_events as $event ) {
setup_postdata( $event );
$event_title = $event->post_title;
$event_id = $event->ID;
$event_permalink = get_post_permalink($event_id);
$event_date = $event->EventStartDate;
$event_date = new DateTime($event_date);
$long_date = $event_date->format('F j, Y \a\t g:i a');
$event_list .= "<li class='event-list-item'>
$long_date - <a href='$event_permalink'>$event_title</a>
</li>";
}
$event_list .= '</ul>';
}
} else {
$event_list = 'Please ensure The Events Tribe Plugin is active.';
}
return $event_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment