Colour events according to venue

By default events are coloured according to their category, however with a just a bit of code you can easily change this behaviour (you can even do this in just the core plug-in installed – but it requires a bit more work!).

As always the following code should (ideally) live in a site utility plug-in, or – less ideally – in your child theme’s functions.php.

First let’s assign the venues a colour – in Pro this is easy – we can make use of the custom fields. We’ll use the HEX format for simplicity. Simply enter a value for the key ‘colour’.

venue-colour

In a future tutorial I’ll show you how you can instead add a colour-picker to the admin page to makes it all a bit more use-friendly.

Once we’ve assigned our venues a colour, the next step is to tell Event Organiser to use that colour. To do this we use the eventorganiser_event_color filter which allows us to change the colour associated with an event.

We’ll do the following:

  • Hook onto that filter
  • Get the associated venue
  • Get the colour of the associated value, and replace the event’s original colour with this one.
  • If the event does not have a venue, or that venue doesn’t have a colour we’ll leave the event’s original colour untouched. We could provide our own ‘fallback’ colour.

The code:

add_filter( 'eventorganiser_event_color', 'my_change_event_color', 10, 2 );
function my_change_event_color( $colour, $post_id ){

   //Get the associated venue
   $venue_id = eo_get_venue($post_id);
   if( $venue_id ){
        //Event has a venue, get its colour:
        $new_colour = eo_get_venue_meta( $venue_id, 'colour', true );

        if( $new_colour ){
             //Venue as a colour stored, use it for the event
             $colour = $new_colour;
        }

   } //If event has venue

   return $colour;
}

But this doesn’t work? For obvious performance reasons the calendars and widgets are cached. Without clearing the cache changes like this will take about 24 hours to propagate through. To clear the cache instantly you can simply re-save an event – your changes should then appear.