Every event its own color!
Events Manager already has a color for each event category, set when you edit the category in Events then Event Categories. Calendars use it to draw each event. If one color per group of events is enough, use categories and skip this tutorial. See Event categories.
Sometimes the color has to be chosen per event, whatever its category. This tutorial does that with an event attribute: an extra field on the event editor whose value you print with a placeholder. It needs no PHP.
The original version of this page, written for Events Manager 3, added a JavaScript color picker to the event editor. That code targeted an admin screen that no longer exists and has been removed.
What you will build
A color menu on the event editor with a fixed list of colors, and an events list where each event's title has a colored bar beside it.
Event attributes must be on: in Events then Settings then General then General Options, set Enable event attributes? to Yes. Editing the list format needs Advanced Mode enabled in Events then Settings then Formatting.
Steps
-
In Events then Settings then General then General Options, find Event Attributes and add this line:
#_ATT{color}{#1d70b8|#d4351c|#00703c|#f47738}The first pair of braces names the attribute. The second lists the allowed values, separated by
|. The first value is the default. -
Save the settings, then edit an event. The Attributes box has a color menu with the four values. Choose one and update the event.
-
In Events then Settings then Formatting then Events, use the attribute in Default event list format, inside a
styleattribute:<li style="border-left: 6px solid #_ATT{color}; padding-left: 0.75rem;">#_EVENTLINK<br />#_EVENTDATES</li> -
Save and view your events list.
How it works
#_ATT{color} prints the value saved on the event. An event that has never had a color chosen prints the default, which is the first value in the list you defined.
Listing the allowed values matters for more than convenience. A value printed inside a style attribute is written by whoever edits the event, and on a site that accepts event submissions that could be a visitor. A menu of fixed choices means only the values you listed can be saved through the form. Don't define a free-text attribute and print it inside style or any other HTML attribute.
To show a name in the menu instead of a color code, define the attribute with names (#_ATT{color}{blue|red|green}) and map them to colors in CSS:
<li class="my-event my-event-color-#_ATT{color}">#_EVENTLINK</li>
.my-event { border-left: 6px solid #999; padding-left: 0.75rem; }
.my-event-color-blue { border-left-color: #1d70b8; }
.my-event-color-red { border-left-color: #d4351c; }
.my-event-color-green { border-left-color: #00703c; }
This version keeps the colors in your stylesheet, where you can change them later without editing every event.
Test it
- Give two events different colors and view the list. Each has its own bar color.
- View an event that has no color set. It uses the first color in your list.