Skip to main content

Create a Custom Placeholder for Event Formatting

Build an add-on

Part 7 of 11 in the Build an add-on series. Previous: Search form fields. Next: Conditional placeholders.

Placeholders are how formats, shortcodes and emails print event details. This part adds #_STYLES, which prints the names of an event's styles, so anyone editing a format can use it without touching PHP.

To change what an existing placeholder prints, see Change placeholder output. It uses the same filter.

What you will build​

A #_STYLES placeholder that prints a comma-separated list such as "Formal, Casual", or "none".

Before you start

Complete part 4, which provides $EM_Event->styles.

Steps​

  1. Add this line inside my_em_styles_init():

    add_filter( 'em_event_output_placeholder', 'my_em_styles_placeholder', 10, 3 );
  2. Add the function below to the end of the file.

Part 7: the #_STYLES placeholder
function my_em_styles_placeholder( $replace, $EM_Event, $full_result ) {
if ( $full_result !== '#_STYLES' ) {
return $replace;
}
$names = array_intersect_key( my_em_styles_get(), array_flip( (array) $EM_Event->styles ) );
return $names ? esc_html( implode( ', ', $names ) ) : esc_html__( 'none', 'my-em-styles' );
}

How it works​

When an event is formatted, Events Manager finds every placeholder in the format, works out a replacement for the ones it knows, and passes each through em_event_output_placeholder. The filter runs for unknown placeholders too, with the placeholder's own text as the starting value, which is what lets you add new ones.

The callback takes three of the filter's five arguments:

ArgumentContains
$replaceThe replacement so far. Return it unchanged for placeholders that aren't yours.
$EM_EventThe event being formatted.
$full_resultThe placeholder as written in the format, for example #_STYLES.
$targetThe kind of output: html, email, rss, ical, map or raw.
$placeholder_attsThe parts of a placeholder that takes arguments in braces, such as #_EVENTEXCERPT{20,...}.

The first line returns early unless the placeholder is exactly #_STYLES. The filter runs for every placeholder of every event on the page, so do the cheap comparison before any other work.

array_intersect_key() picks the names of this event's styles from the full list, which also drops IDs of styles that have since been deleted. The result is escaped with esc_html() because the replacement is inserted into the page as it is.

A placeholder name starts with #_ and can contain letters, digits and underscores. The built-in ones are written in capitals. Choose a name with your own prefix if there is any chance of clashing with a future built-in one.

Test it​

  1. Add this shortcode to a page:

    [events_list]<p>#_EVENTLINK: #_STYLES</p>[/events_list]
  2. Each event shows its styles after its name, and events with none show "none".

  3. Add #_STYLES to the Single event page format in Events then Settings then Formatting then Events to print it on event pages. The format box is editable once Advanced Mode is enabled at the top of that tab.

Next​

Part 8: Conditional placeholders