Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how you can create a custom shortcode to pull values from EE events.
<?php
/*
Plugin Name: Event Espresso 4 custom value shortcode
Description: Adds a [EE_CUSTOM_VALUE_SHORTCODE] shortcode to pull various values from the event.
Author: Tony Warwick
Version: 1.0
Author URI: https://github.com/Pebblo
*/
add_shortcode('EVENT_ESPRESSO_EVENT_VENUE_SHORTCODE', 'tw_ee_event_venue_shortcode');
function tw_ee_event_venue_shortcode( $atts ){
// Setup the attributes for this shortcode.
$atts = shortcode_atts(
array(
'event_id' => null,
),
$atts,
'EVENT_ESPRESSO_EVENT_VENUE_SHORTCODE'
);
// Use the EE event models to pull an event using the event_id value.
$event = EEM_Event::instance()->get_one_by_ID($atts['event_id']);
// Check we have an EE Event object.
if( $event instanceof EE_Event ) {
// Pull the venue object from the event.
$evt_venue = $event->venues();
$evt_venue = ! empty($evt_venue) ? array_shift($evt_venue) : null;
// Check we have a valid venue before using it.
if( $evt_venue instanceof EE_Venue ) {
// Return the venue name.
return $evt_venue->name();
}
}
// If we have nothing to return, just return and emprty string.
return '';
}
add_shortcode('EVENT_ESPRESSO_EVENT_DATETIME_SHORTCODE', 'tw_ee_event_datetime_shortcode');
function tw_ee_event_datetime_shortcode( $atts ){
// Setup the attributes for this shortcode.
$atts = shortcode_atts(
array(
'event_id' => null,
),
$atts,
'EVENT_ESPRESSO_EVENT_DATETIME_SHORTCODE'
);
// Use the EE event models to pull an event using the event_id value.
$event = EEM_Event::instance()->get_one_by_ID($atts['event_id']);
// Check we have an EE Event object.
if( $event instanceof EE_Event ) {
// Pull the 'primary_datetime' from the event.
$primary_datetime = $event->primary_datetime();
// Make sure we actually have an EE_Datetime object.
if( $primary_datetime instanceof EE_Datetime ) {
// Now output the start date.
return $primary_datetime->start_date();
}
}
// If we have nothing to return, just return and emprty string.
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment