One of the new features that is included in Event Espresso 3.1 is the EE_ANSWER shortcode. This shortcode makes it possible to grab answers to custom questions without writing your own custom database queries. It’s been developed to work with the [LISTATTENDEES] shortcode to create customized attendee lists.
Unlike many of the of the other shortcodes that ship with Event Espresso, this one is a bit different in that it’s designed primary to go into a display template. So in order to take advantage of this new feature, a little familiarity with a code editor and an FTP client is required.
First you’ll need a custom question to plug this into. Navigate to the manage questions screen and go to edit the custom question you’d like to include the answer to in the attendee list. Take note of the ID in the URL. This is how the answer will be identified in the next step.
Copy the attendee_list.php template file from the Event Espresso plugin’s /template directory over to /wp-content/uploads/espresso/templates. Open this file in your text editor of choice. Near the bottom of this template, you’ll find a block of PHP code that looks something like this:
<?php
$a_sql = "SELECT * FROM " . EVENTS_ATTENDEE_TABLE . " WHERE event_id='" . $event_id . "'";
$a_sql .= $paid_only == 'true'? " AND (payment_status='Completed' OR payment_status='Pending') ":'';
$a_sql .= $sort;
//echo $a_sql;
$attendees = $wpdb--->get_results($a_sql);
foreach ($attendees as $attendee){
$id = $attendee->id;
$lname = $attendee->lname;
$fname = $attendee->fname;
$city = $attendee->city;
$state = $attendee->state;
$country = $attendee->state;
$email = $attendee->email;
$gravatar = $show_gravatar == 'true'? get_avatar( $email, $size = '100', $default = 'http://www.gravatar.com/avatar/' ) : '';
$city_state = $city != '' || $state != '' ? '
' . ($city != '' ? $city :'') . ($state != '' ? ', ' . $state :' ') :'';
//These are example variables to show answers to questions
$custom_question_1 = '
'.do_shortcode('[EE_ANSWER q="15" a="'.$id.'"]');
//$custom_question_2 = '
//'.do_shortcode('[EE_ANSWER q="13" a="'.$id.'"]');
?>
Note where we’ve uncommented out one of the examples of the EE_ANSWER shortcodes. Also note that the question ID has been placed between double quotes for the q parameter. The final step is to add the newly created custom question variable to the markup. In this example, we’ll add it near the end of the echo statement that’s already there:
<div class="espresso_attendee"><?php echo $gravatar ?><?php echo '<p><strong>' . stripslashes_deep($fname . ' ' . $lname) . '</strong><br />' . ($city != '' ? $city :'') . ($state != '' ? ', ' . $state :' ') . ($custom_question_1 != '' ? $custom_question_1 :'') . '</p>'; ?> </div>
Then add the [LISTATTENDEES] shortcode to page, post, or template with the event_identifier parameter to display a list of attendees along with their specified answer(s).
Tickets
If you want to add the answers to custom questions to tickets created by the Ticketing add on, you can add the following snippet to your ticket template:
<?php echo do_shortcode( '[EE_ANSWER q="10" a="'.$data->attendee->id.'" ]' ); ?>
Simply change the 10 in q=”10″ to the ID for the question you want the to display the answer of.