Tagged: id

Event Espresso 4 – Helpful Functions

event-espresso-logo-600x213-black1-600x198

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>";

 

More

Dynamically Naming Elements in Nested Loops

Wanted to share a technique I use quite a lot at the moment for dynamically naming elements in a loop or, more specifically for this method, in multiple nested loops.

Consider the following code which is pretty standard for most jquery modals at present.

<p>

  <a href="#modal_1" rel="modal:open">Open Modal</a>

</p>

<div id="modal_1" style="display:none;">

  <p>This is the content of the modal</p>

</div>

To render these modals in a loop you might be familiar with the following code which I have seen a million times.  This set up makes sure that each link points to a corresponding container div with a matching unique id. Fine and dandy:

<?php $counter = 1; ?>

<?php foreach($modals as $modal):?>

  <p>

    <a href="#modal_<?php echo $counter; ?>" rel="modal:open">Open Modal</a>

  </p>

  <div id="modal_<?php echo $counter; ?>" style="display:none;">

    <p>This is the content of the modal</p>

  </div>

  <?php $counter++; ?>

<?php endforeach ?>

The problem with this method occurs when you are using this type of markup as a function or if you have some sort of dynamic setup where the above code may be outputted on the same page more than once. This would break the code as the link would just point to the first container div with that name.

I’ve seen setups with some very strange methods to get round this.

The Solution

Here is a simple solution that utilises php’s  uniqid function. This ensures you will NEVER get two id’s matching no mater how many nested loops you have going.

Implement it like so:

<?php foreach($modals as $modal):?>
  
  <?php $unique_id = uniqid(); ?>

  <p>

    <a href="#<?php echo $unique_id; ?>" rel="modal:open">Open Modal</a>

  </p>

  <div id="<?php echo $unique_id; ?>" style="display:none;">

    <p>This is the content of the modal</p>

  </div>

<?php endforeach ?>

If you want to store the id’s of these for use at a later point, like I’ve done recently, you can do this:

<?php $storedids = array(); ?>

<?php foreach($modals as $modal):?>
  
  <?php 
    
      $unique_id = uniqid(); 
      $storedids[] = $unique_id; ?>

  <p>

    <a href="#<?php echo $unique_id; ?>" rel="modal:open">Open Modal</a>

  </p>

  <div id="<?php echo $unique_id; ?>" style="display:none;">

    <p>This is the content of the modal</p>

  </div>

<?php endforeach ?>

Then you have access to the id’s later like so:

<?php 
  foreach($storedids as $storedid){
       echo $storedid;
  }
?>

I’ve used this not just with modals, but when generating unique ids for other jQuery functions, when using with Advanced Custom Fields ‘Flexible Field’ addon to make sure all generated sections of content have a nice unique ids to point to.

More