Skip to content

Instantly share code, notes, and snippets.

@amberhinds
Created October 4, 2018 03:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amberhinds/9675d5cffd28ec875e7ae8e763cd80ea to your computer and use it in GitHub Desktop.
Save amberhinds/9675d5cffd28ec875e7ae8e763cd80ea to your computer and use it in GitHub Desktop.
Display upcoming events from meetup in a shortcode
<?php
/**
* Shortcodes
*
* @package CoreFunctionality
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
/**
* Upcoming Meetups
*
*/
function ea_upcoming_meetups_shortcode( $atts = array() ) {
$atts = shortcode_atts( array(
'count' => 5,
'meetup' => 'georgetown-tx-wordpress',
), $atts, 'upcoming_meetups' );
$key = 'upcoming_meetups_' . sanitize_title( $atts['meetup'] ) . '_' . intval( $atts['count'] );
$output = get_transient( $key );
if( false === $output ) {
$query_args = array(
'sign' => 'true',
'photo-host' => 'public',
'page' => intval( $atts['count'] ),
);
$request_uri = 'https://api.meetup.com/' . sanitize_title( $atts['meetup'] ) . '/events';
$request = wp_remote_get( add_query_arg( $query_args, $request_uri ) );
if( is_wp_error( $request ) || '200' != wp_remote_retrieve_response_code( $request ) )
return;
$events = json_decode( wp_remote_retrieve_body( $request ) );
if( empty( $events ) )
return;
$output = '<ul>';
foreach( $events as $event ) {
$date = strtotime( $event->local_date . ' ' . $event->local_time );
$output .= sprintf(
'<li><a href="%s">%s</a> on %s at %s</li>',
esc_url_raw( $event->link ),
esc_html( $event->name ),
date( 'F jS', $date ),
date( 'g:ia', $date )
);
}
$output .= '</ul>';
set_transient( $key, $output, DAY_IN_SECONDS );
}
return $output;
}
add_shortcode( 'upcoming_meetups', 'ea_upcoming_meetups_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment