Last active
October 13, 2021 17:01
-
-
Save Pebblo/b2a0c9e1bf17b78f04e378b92468daa2 to your computer and use it in GitHub Desktop.
Example of how you can create a custom shortcode to pull values from EE events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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