Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Last active June 12, 2017 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pebblo/e87cc8e30c4848dcdfe2 to your computer and use it in GitHub Desktop.
Save Pebblo/e87cc8e30c4848dcdfe2 to your computer and use it in GitHub Desktop.
Add custom shortcodes within EE_Event_Shortcodes and EE_Datetime_shortcodes available within the EE Messages system.
<?php
/*
Plugin Name: EE Custom Messages Shortcode - Site Specific Plugin
Description: Add custom messages shortcodes for Event Espresso 4 here.
*/
/* Begin Adding Functions Below This Line; Do not include an opening PHP tag as this sample code already includes one! */
function register_new_tony_shortcodes( $shortcodes, EE_Shortcodes $lib ) {
//Add a shortcode to be used with the EE Datetimes within messages
if ( $lib instanceof EE_Datetime_Shortcodes ) {
//Add your shortcode to the add as the key, the value should be a description of the shortcode.
$shortcodes['[TONYS_DATETIME_SHORTCODE]'] = _('This is Tonys Custom DATETIME Shortcode!');
}
//Add a shortcode to be used with the EE Event List within messages
if ( $lib instanceof EE_Event_Shortcodes ) {
//Add your shortcode to the add as the key, the value should be a description of the shortcode.
$shortcodes['[TONYS_EVENT_SHORTCODE]'] = _('This is Tonys Custom EVENT Shortcode!');
}
//Return the shortcodes.
return $shortcodes;
}
add_filter( 'FHEE__EE_Shortcodes__shortcodes', 'register_new_tony_shortcodes', 10, 2 );
function register_new_tony_shortcodes_parser( $parsed, $shortcode, $data, $extra_data, EE_Shortcodes $lib ) {
//Check for the datetime shortcodes as that's were we added the custom shortcode above
//also check that $data is the expected object (in this case an EE_Datetime)
if ( $lib instanceof EE_Datetime_Shortcodes && $data instanceof EE_Datetime ) {
//Then check for our specific shortcode
if ( $shortcode == '[TONYS_DATETIME_SHORTCODE]' ) {
//Do whatever you need to do here and return the value for that specific datetime.
return $data->ID();
}
}
if ( $lib instanceof EE_Event_Shortcodes ) {
//Then check for our specific shortcode
if ( $shortcode == '[TONYS_EVENT_SHORTCODE]' ) {
//First check we have an event object.
$event = $data instanceof EE_Event ? $data : null;
//if no event, then let's see if there is a reg_obj. If there IS, then we'll try and grab the event from the reg_obj instead.
if ( empty( $event ) ) {
$aee = $data instanceof EE_Messages_Addressee ? $data : NULL;
$aee = $extra_data instanceof EE_Messages_Addressee ? $extra_data : $aee;
$event = $aee instanceof EE_Messages_Addressee && $aee->reg_obj instanceof EE_Registration ? $aee->reg_obj->event() : NULL;
}
//Check we do now actually have the event object.
if ( !empty( $event ) ) {
//Do whatever you need to do using the event object, which is now $event and return.
return $event->ID();
}
}
}
//If not within the correct section, or parsing the correct shortcode,
//Return the currently parsed content.
return $parsed;
}
add_filter( 'FHEE__EE_Shortcodes__parser_after', 'register_new_tony_shortcodes_parser', 10, 5 );