Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active April 26, 2024 17:43
Show Gist options
  • Save andrasguseo/685895d0820c8d9f90726071613f1c64 to your computer and use it in GitHub Desktop.
Save andrasguseo/685895d0820c8d9f90726071613f1c64 to your computer and use it in GitHub Desktop.
ET+ > Show the collected attendee information on the Attendees page in a column
<?php
/**
* Description: Add a column with attendee information to the Attendees admin page in Event Tickets
* Usage: Paste the below snippet into your active (child) theme's functions.php file
* or use a plugin like Code Snippets.
*
* Plugin: Event Tickets, Event Tickets Plus
* Author: Andras Guseo
* Last updated: 2024-04-26
*/
/**
* Add an extra column header for attendee information
*
* @param array<string,string> $columns An array of column headers.
*
* @return array
*/
function tec_et_attendee_info_column( $columns ) {
/**
* Choose below after which column you would like to add this column
* 'cb', 'ticket', 'primary_info', 'security', 'status', 'check_in'
*/
$insert_after_column = 'primary_info';
foreach ( $columns as $key => $value ) {
$new_columns[ $key ] = $value;
if ( $key == $insert_after_column ) {
$new_columns[ 'attendee_info' ] = "Attendee Info";
}
}
return $new_columns;
}
add_filter( 'tribe_tickets_attendee_table_columns', 'tec_et_attendee_info_column' );
/**
* Add values to the attendee information column
*
* @param string $value The information shown in the column.
* @param \WP_Post $item row object.
* @param string $column the column name.
*
* @return string
*/
function tec_et_attendee_info_column_data( $value, $item, $column ) {
// Bail if not the right column.
if ( $column !== 'attendee_info' ) {
return $value;
}
// Toggle functionality
// - true: toggle like originally, start hidden
// - false: always show, no toggle
$iwanttoggle = false;
$info = '';
// Add extras if toggle is needed.
if ( $iwanttoggle ) {
$info = '<div><span class="etattinfo--toggle" style="cursor:pointer; float:left;margin-right:1em;">▼</span><div style="float:left;display:none;">';
}
// Get the attendee meta.
if ( is_array( $item[ 'attendee_meta' ] ) ) {
foreach ( $item[ 'attendee_meta' ] as $key => $value ) {
if ( empty( $value ) ) {
continue;
}
// Tickets Commerce ticket
if ( isset( $item['post_type'] ) && $item['post_type'] === 'tec_tc_attendee' ) {
// Create a label from the slug.
// If it's a checkbox, remove the random number.
if ( strlen( $key ) > 32 ) {
$label = explode( '_', $key );
$label = esc_html( $label[0] );
} else {
$label = esc_html( $key );
}
// Label + value
$info .= ucwords( $label ) . ": " . $value;
} else {
// RSVP, Woo Ticket
$info .= esc_html( $value[ 'label' ] ) . ": ";
// If it's a checkbox, then it's an array, so make a list.
if ( is_array( $value[ 'value' ] ) ) {
$info .= esc_html( implode( ',', $value[ 'value' ] ) );
} else {
$info .= esc_html( $value[ 'value' ] );
}
}
$info .= '<br>';
}
}
// Add extras if toggle is needed.
if ( $iwanttoggle ) {
$info .= "</div></div>";
}
return $info;
}
add_filter( 'tribe_events_tickets_attendees_table_column', 'tec_et_attendee_info_column_data', 10, 3 );
/**
* Add script for toggle functionality.
* Can be removed if toggle functionality is not needed.
* In that case set $iwanttoggle = false above.
*
* @return void
*/
function etattinfoscript() {
if ( get_current_screen()->id == 'tribe_events_page_tickets-attendees' ) {
?>
<script id="et-attendee-information-toggle-script">
document.querySelectorAll('.etattinfo--toggle').forEach(function (toggle) {
toggle.addEventListener('click', function () {
const box = this.nextElementSibling;
if (box.style.display == null || box.style.display === 'block') {
box.style.display = 'none';
} else {
box.style.display = 'block';
}
});
});
</script>
<?php
}
}
add_action( 'admin_footer', 'etattinfoscript' );