Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.