I’ve recently been working on a project using Event Espresso 4 and I’ve had to customize it quite extensively. I wrote a few functions that I think might be handy for others so I thought I would share them:
Get Event ID From Registration Code
I wrote this one to help me get the event information when a user clicks on a ‘cancel registration’ link. The reg code is more secure way to pass as a GET variable in a link than the user/event/registration id.
The Function
function get_eventid_of_reg_id($regid){ global $wpdb; $query = "SELECT EVT_ID from wp_esp_registration where REG_code = '".$regid."'"; $result = $wpdb->get_row($query); $result = $result->EVT_ID; return $result; }
Usage Example
$regid = "axssa-12343"; $eventid = get_eventid_of_reg_id($regid); $event_info_Object = get_post($eventid); // Returns a WP_Post object https://codex.WordPress.org/Class_Reference/WP_Post
Get Event Date
This one is handy if you have the event id and you are just looking for the date of the event.
The Function
function get_event_date($eventid, $format){ global $wpdb; $query = "SELECT DTT_EVT_start from wp_esp_datetime where EVT_ID = '".$eventid."'"; $result = $wpdb->get_row($query); $result = $result->DTT_EVT_start; $phpdate = strtotime( $result ); $mysqldate = date( $format, $phpdate ); return $mysqldate; }
Usage Example
$event_id = 34; $event_date = get_event_date($event_id, 'Y-m-d'); echo $event_date;
Get the Status of the Registration From Registration Code
This one can be used to see the status of a particular registration. I wrote it to provide confirmation that a registration had been cancelled in a custom built ‘cancellation journey’.
The Function
function get_regid_status_from_regcode($regid){ global $wpdb; $query = "SELECT STS_ID from wp_esp_registration where REG_code = '".$regid."'"; $result = $wpdb->get_row($query); $result = $result->STS_ID; return $result; }
This function returns a status code:
“RAP” (registration status of approved. This is the default behavior.)
“RCN” (registration status of cancelled.)
“RDC” (registration status of declined.)
“RIC” (registration status of incomplete.)
“RNA” (registration status of not approved.)
“RPP” (registration status of pending payment.)
Usage Example
$registration_code = "j9ojjsk-ookkd1"; $registration_status = get_regid_status_from_regcode($registration_code); if($registration_status == "RCN"){ echo "This registration is cancelled"; }
Get the Status of the Event From Registration ID
Same as above just using the Registration ID.
The Function
function get_regid_status_from_id($regid){ global $wpdb; $query = "SELECT STS_ID from wp_esp_registration where REG_ID = '".$regid."'"; $result = $wpdb->get_row($query); $result = $result->STS_ID; return $result; }
This function returns a status code:
“RAP” (registration status of approved. This is the default behavior.)
“RCN” (registration status of cancelled.)
“RDC” (registration status of declined.)
“RIC” (registration status of incomplete.)
“RNA” (registration status of not approved.)
“RPP” (registration status of pending payment.)
Usage Example
$registration_id = 72; $registration_status = get_regid_status_from_regcode($registration_id); if($registration_status == "RCN"){ echo "This registration is cancelled"; }
Get Registrant Details From Registration ID
This function gives you the all the registration details of a Registrant if you have the registration ID.
The Function
function get_registrant_from_reg_id($regid){ global $wpdb; $query = "SELECT wp_esp_registration.ATT_ID, ATT_fname as 'first_name', ATT_lname as 'last_name', ATT_email as 'email' from wp_esp_registration join wp_esp_attendee_meta on wp_esp_attendee_meta.ATT_ID = wp_esp_registration.ATT_ID where REG_ID = '".$regid."'"; $result = $wpdb->get_row($query); return $result; }
This function returns an object with ‘first_name’, ‘last_name’ and ’email’ as the parameters.
Usage Example
$registration_id = 42; $registration_detail = get_registrant_from_reg_id($registration_id); echo "The registrants first name is ". $registration_detail->first_name. "<br>"; echo "The registrants last name is ". $registration_detail->last_name. "<br>"; echo "The registrants email adddress is ". $registration_detail->email. "<br>";
Hey Mario, great work on figuring out functions to use for your needs! I’m one of the core developers of EE and just thought I’d give some pointers to you and your readers on what you may find to be an easier way of retrieving EE data.
For more details, you can see some documentation on our model system here: http://developer.eventespresso.com/docs/model-querying/ but I’ll go through each of your examples to show you how to use the model system to get what you want:
1. Get Event ID from registration code.
2. Get Event Date
There’s actually already a helper function in EE files for returning the primary date on an event (in EE4 an event can have multiple dates attached to it so this is handy method for returning not only the primary date but also in the format you want AND localized.
You can find the above function and others in “public/template_tags.php”
3. Get Status of Event from Registration Code.
In your example, your actually not getting the status of the event, but you are retrieving the status of the registration.
To get the status of the event you can do this:
But if you DO want the registration status, here’s a method to retrieve that (along with retrieving the entire registration object for other stuff you can get.
Retrieving EE objects this way is super helpful because you can access other properties of the project quite easily. In the above example you’ll retrieve the “pretty” label for the status (which is also localized).
4. Get Registrant Details from Registration ID
Very similar to the above example:
Hope this helps 🙂
WOW. Thank you so much for that. I had no idea about that developer portal web site. How did I miss it? Thanks for your examples. I’m not the greatest using OOP classes and methods but will be sure to delve into this.
Hi Mario,
ANOTHER EE dev here…
Regardless of whether accessing EE data or not, you should at least use WPDB->prepare() in your queries to protect yourself from SQL injection attacks;
so instead of :
which could result in a serious security compromise if $regid is being obtained from $_REQUEST data…
you should do the following:
For more information, plz see:
https://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
Thanks for the info Brent. I’ve updated the function on my EE theme to account for this.
This was extremely helpful and saved me a lot of time and money as a WordPress Developer and an EE4 Custom Plugin Developer. I was stuck on something for hours. Came across this.. solved in minutes! Thank you so much!