Colour-Code Event Categories in Calendar Views
Adds a coloured left border to events in The Events Calendar's list, day and month views based on their event category, so visitors can scan the calendar by type.
Opens WPIDE on
Code
$wpide_colors = array( // Event category slug => colour.
'workshops' => '#2271b1',
'concerts' => '#d63638',
'kids' => '#00a32a',
);
add_action( 'wp_head', function () use ( $wpide_colors ) {
if ( ! class_exists( 'Tribe__Events__Main' ) || ! function_exists( 'tribe_is_event_query' ) || ! tribe_is_event_query() ) {
return;
}
$css = '';
foreach ( $wpide_colors as $slug => $color ) {
$slug = sanitize_html_class( $slug );
$color = sanitize_hex_color( $color );
if ( $slug && $color ) {
$css .= sprintf(
'.tribe-events .tribe_events_cat-%1$s,.tribe-events .tribe-events-calendar-month__calendar-event.tribe_events_cat-%1$s{border-inline-start:4px solid %2$s;padding-inline-start:8px}',
$slug,
$color
);
}
}
if ( $css ) {
echo '<style id="wpide-tec-cat-colors">' . $css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput -- built from sanitized parts.
}
} );
Notes
Map category slugs to colours in $wpide_colors. Uses the category classes TEC adds to each event.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.