Code Snippets

You can extend your Event Espresso 4 powered event registration system with any of the code snippets below. Unless otherwise indicated, we recommend placing code snippets within a plugin.

You can find many more snippets in our library hosted on Github.

Add events to any blog archive listing and rss feed

Below is the code to add that will inject the “espresso_events” custom post type into the WordPress WP_Query post_types array. This allows Event Espresso Events to appear in any blog archive listing and rss feed.

<?php
/**
 * This is an alternative filter for adding events to posts.
 *
 * 1. Adds events to ANY query for posts (including core wp functions like wp_recent_posts() that suppress filters ).
 * 2. Ensures we don't accidentally add posts (and events) into custom queries for other post types. (eg sliders).
 */

function add_espresso_events_to_posts( $WP_Query ) {
	if ( $WP_Query instanceof WP_Query && ( $WP_Query->is_feed || $WP_Query->is_posts_page  || ( $WP_Query->is_home && ! $WP_Query->is_page ) ||  ( isset( $WP_Query->query_vars['post_type'] ) && ( $WP_Query->query_vars['post_type'] == 'post' || is_array( $WP_Query->query_vars['post_type'] ) && in_array( 'post', $WP_Query->query_vars['post_type'] ) ) ) ) ) {
		//if post_types ARE present and 'post' is not in that array, then get out!
		if ( isset( $WP_Query->query_vars['post_type'] ) && $post_types = (array) $WP_Query->query_vars['post_type'] ) {
			if ( ! in_array( 'post', $post_types ) ) {
				return;
			}
		} else {
			$post_types = array( 'post' );
		}

		if ( ! in_array( 'espresso_events', $post_types )) {
			$post_types[] = 'espresso_events';
			$WP_Query->set( 'post_type', $post_types );
		}
		return;
	}
}
add_action( 'pre_get_posts', 'add_espresso_events_to_posts', 10 );

Visit the row on Git: https://gist.github.com/nerrad/9ec4e10cc4ba16650484#file-filter-events-to-posts-php

Add a category class name to the post class

Below is the code to add that will add a class name to the Event Espresso event post’s post class so events that belong to a specific category can be uniquely styled with CSS.

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

// Add a class name to the event post's post class that's based on the category it's assigned to

add_filter( 'post_class', 'ee_add_taxonomy_post_class', 10, 3 );

function ee_add_taxonomy_post_class( $classes, $class, $ID ) {
    $taxonomy = 'espresso_event_categories';
    $terms = get_the_terms( (int) $ID, $taxonomy );
    if( !empty( $terms ) ) {
        foreach( (array) $terms as $order => $term ) {
            if( !in_array( $term->slug, $classes ) ) {
                $classes[] = $term->slug;
            }
        }
    }
    return $classes;
}

Visit the row on Git: https://gist.github.com/joshfeck/11383932#file-ee_add_taxonomy_post_class-php

Deactivate the iCal functionality that was added in Event Espresso 4.3

Below is the code to add that will remove the iCal link that was added in Event Espresso 4.3

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

add_action( 'template_redirect', 'my_remove_ical_link' );

function my_remove_ical_link() {
    remove_filter( 'FHEE__espresso_list_of_event_dates__datetime_html', array( 'EED_Ical', 'generate_add_to_iCal_button' ), 10 );
}

Visit the row on Git: https://gist.github.com/joshfeck/1bc3731cd1c468916c7f#file-functions-php

Display the base price of the ticket in the ticket selector

Below is the code to add that will change the display of the ticket selector so it displays a base price (instead of the modified price). Requires Event Espresso 4.4 or greater.

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

// display the base price of the ticket in the ticket selector (instead of the modified price)

add_filter(
    'FHEE__ticket_selector_chart_template__ticket_price',
    'change_ee_ticket_selector_base_price_display',
    10,
    2
);

function change_ee_ticket_selector_base_price_display(
    $ticket_price,
    $ticket
) {
    return $ticket->base_price()->amount();
}

Visit the row on Git: https://gist.github.com/joshfeck/8736a20cbfacf5d5b97a#file-base_price-php

Display remaining ticket inventory total for each datetime

Below is the code to add that will add a “Total tickets remaining: x” message below each event’s datetime.

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

add_filter(
  'FHEE__espresso_list_of_event_dates__datetime_html',
  'my_add_total_spaces_display', 
  10, 
  2
);
function my_add_total_spaces_display( $html, $datetime ) {
  $html .= '<br><span class="space-remain">Total spaces remaining: ';
  $html .= $datetime->total_tickets_available_at_this_datetime();
  $html .= '</span>';
  return $html;
}

Visit the row on Git: https://gist.github.com/joshfeck/f706dd0c1d315289c7293b24a5fa9c02#file-add_total_spaces_available-php

Print an attendee list after the content on the event details page

Below is the code to add that will print an attendee list after the content on the event details page. Displays the gravatar, first name, and last name of each registered attendee for an event.

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

// prints a per event attendee list right after the event details on a single event page
// you can change the action hook that's on the next line to display the list in another location

add_action( 'AHEE_event_details_after_the_content', 'ee_attendee_list' );

function ee_attendee_list(){
    global $wpdb, $post;
    
    if(is_singular()){
        echo '<ul class="attendee-list">';
    
        $sql = "SELECT ATT_fname, ATT_lname, ATT_email ";
        $sql .= "FROM {$wpdb->prefix}esp_attendee_meta ";
        $sql .= "INNER JOIN {$wpdb->prefix}esp_registration ";
        $sql .= "ON {$wpdb->prefix}esp_attendee_meta.ATT_ID = {$wpdb->prefix}esp_registration.ATT_ID ";
        $sql .= "WHERE {$wpdb->prefix}esp_registration.EVT_ID = %d";
        
        $attendees = $wpdb->get_results( $wpdb->prepare( $sql, $post->ID ));
        foreach($attendees as $attendee){
            $fname = $attendee->ATT_fname;
            $lname = $attendee->ATT_lname;
            $email = $attendee->ATT_email;
            $gravatar = get_avatar( $email, '64', 'identicon' );
            $html = '<li>'. $gravatar .'<span>'. $fname .' '. $lname . '</span></li>';
            echo $html;
        }
    
        echo '</ul>';
    }
}

Visit the row on Git: https://gist.github.com/joshfeck/55f20059953119b46b34#file-attendee_list-php

Exclude events from the event list that have no tickets on sale

Below is the code to add that will remove events from the event list that do not have tickets on sale.
Get the code on Github.com

Print a list of upcoming events for a venue on the venue details page

Below is the code to add that will print a list of upcoming events for a venue after the content on the venue details page. Includes the permalink and event name.

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

function ee_list_upcoming_events_for_a_venue( $post ) {
    // query the events for this venue using EE_Venue's events() method
    $query_params = array( 
        'order_by' => 'Datetime.DTT_EVT_start',
        'order' => 'ASC',
        array( 
            'status' => 'publish', 
            'Datetime.DTT_EVT_start' => array( 
                '>', 
                date( current_time( 'mysql' ) ), 
                'Datetime.DTT_EVT_end' => array(
                    '<', 
                    date( current_time( 'mysql' ) )
                ) 
            )
        )
    );
    $events = EEH_Venue_View::get_venue( $post->ID )->events( $query_params );

    // start the output
    echo '<h3>Upcoming events at this venue</h3><ul>';
    // the loop
    foreach ( $events as $event ) {
        echo '<li>';
        echo '<a href="' . get_permalink( $event->get( 'EVT_ID' ) ) . '">' . $event->get( 'EVT_name' ) . '</a>';
        echo '</li>';
    }
    echo '</ul>';    
    // end the output
}

// next, add the above to an action found in the venue template
// you could instead use 'AHEE__content_espresso_venues_details_template__before_the_content' to put the list before the content
add_action( 'AHEE__content_espresso_venues_details_template__after_the_content', 'ee_list_upcoming_events_for_a_venue' );

Visit the row on Git: https://gist.github.com/joshfeck/0622e324fb3729880f67#file-events_venue-php

Remove “powered by Event Espresso” admin footer text in Event Espresso 4

Below is the code to add that will remove the “powered by Event Espresso” admin footer text in Event Espresso 4.

<?php
// Please do NOT include the opening php tag, except of course if you're starting with a blank file
// remove "powered by Event Espresso" admin footer text

add_filter( 'admin_footer_text', 'ee_remove_footer_text', 11 ); 

function ee_remove_footer_text() {
    remove_filter( 'admin_footer_text', array( 'EE_Admin', 'espresso_admin_footer' ), 10 );
} 

Visit the row on Git: https://gist.github.com/joshfeck/2b124ac5bbfa3d529f0b#file-remove_powered_by-php

Exclude password protected events in EE4

This script will exclude password protected events and posts from the WordPress loop. Adapted from Stackoverflow: http://stackoverflow.com/questions/7538959/how-to-exclude-password-protected-posts-in-wordpress-loop

<?php 
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

// Create a new filtering function that will add our where clause to the query
function ee_password_post_filter( $where = '' ) {
    // Make sure this only applies to loops / feeds on the frontend
    if (!is_single() && !is_admin()) {
        // exclude password protected
        $where .= " AND post_password = ''";
    }
    return $where;
}
add_filter( 'posts_where', 'ee_password_post_filter' );

Visit the row on Git: https://gist.github.com/sethshoultes/24d5540575920e5fd7eb#file-ee_password_post_filter-php

Yoast’s WordPress SEO and EE4 Metabox fix

This may now be resolved as WordPress SEO has changed its hook, but if you have WordPress SEO installed and EE4 metaboxes disappear from the Event editor page, try using the code published in our snippets library:

<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file

//This function allows Yoast SEO to load within Event Espresso when you edit or create any event or venue.
function ee_yoast_seo_always_register_metaboxes_in_admin(){
	global $pagenow;
	$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
	$action = isset( $_GET['action'] ) ? $_GET['action'] : '';

	if ( $pagenow == 'admin.php' && ( $page == 'espresso_events' || $page == 'espresso_venues' )  && ( $action == 'edit' || $action == 'create_new' ) ) {
		return true;
	}
	return false;
}
add_filter( 'wpseo_always_register_metaboxes_on_admin', 'ee_yoast_seo_always_register_metaboxes_in_admin' );

Visit the row on Git: https://github.com/eventespresso/ee-code-snippet-library/blob/master/third-party-integration/tw_ee_load_yoast_seo_within_ee.php

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us