Creating Conditional Placeholders for Events

In our previous tutorial in this series we created a custom placeholder for our “Styles” example add-on for Events Manager. We will now do something similar but with a very different purpose, add CONDITIONAL placeholders for our events.

Each event can have a unique combination of certain enabled/disabled features such as bookings, event prices, attributes, recurrences etc. The problem with placeholders and the use of event formats is that without using PHP, we previously had no way of disabling certain content such as removing booking forms and buttons if there are no bookings. Events Manager 4.0 allows custom placeholders such as {has_bookings}this, and these contents would only appear if my event had bookings{/has_bookings}.

We would really benefit from having custom placeholders like that so we can tell if our event has a specific style. Note the last bit of that sentence… SPECIFIC style. We’ll add a twist here to show how flexible this can be with a little creativity.

add_action('em_event_output_condition', 'my_em_styles_event_output_condition', 1, 4);
function my_em_styles_event_output_condition($replacement, $condition, $match, $EM_Event){
	if( is_object($EM_Event) && preg_match('/^has_style_(.+)$/',$condition, $matches) && is_array( $EM_Event->styles ) ){
		if( in_array($matches[1],$EM_Event->styles) ){
			$replacement = preg_replace("/\{\/?$condition\}/", '', $match);
			$replacement = '';
	return $replacement;

We begin by hooking to the em_event_output condition (note that same principle would apply for locations and categories).

if( is_object($EM_Event) && preg_match('/^has_style_(.+)$/',$condition, $matches) && is_array( $EM_Event->styles ) ){

This is the most important line in our function. To avoid any runtime errors, we’ll just return the original replacement value if the $EM_Event isn’t an object. The preg_match method runs a regex matching operation to make sure we’re dealing with a conditional placeholder with the name of has_style_x , where x is the id number of that style. The last function is just a precaution to avoid unexpected PHP warnings in case the styles weren’t loaded correctly, but we could probably even do without it (as a plugin dev it’s now a habit for me).

This regex also stores the found number in the $matches array, which we use in the next line to check if this event has this style id in its styles array. If it does, we remove the start and end placeholder tags with a preg_replace statement and return the contents, otherwise we just return a blank string.

You can use this function within any formats that accept Event placeholders from within your settings page, shortcode, template tags or directly accessing the event objects. In our next tutorial, we’re going to create a custom event information page which will show a list of styles and display the upcoming events under each style, as well as our single style pages.