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 regular placeholders and the use of event formats is that without using PHP, we have no way of disabling certain content such as removing booking forms and buttons if there are no bookings. Custom placeholders such as {has_bookings}
solves this problem, and this bit of text between the conditional placeholder would only appear if my event had bookings {/has_bookings}
.
Creating our own Conditional Placeholder
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_show_condition', 'my_em_styles_event_output_show_condition', 1, 4);
function my_em_styles_event_output_show_condition($show, $condition, $full_match, $EM_Event){
if( !empty( $EM_Event->styles ) && preg_match('/^has_style_(.+)$/',$condition, $matches) ){
if( is_array($EM_Event->styles) && in_array($matches[1],$EM_Event->styles) ){
$show = true;
}
}
return $show;
}
We begin by hooking to the em_event_output_show_condition
, which is used to decide whether or not this content wrapped within the condition will be shown or not. Note that same principle would apply for any other objects that format output and accepts conditional placeholders, such as EM_Locations and have similar naming conventions, such as em_location_output_show_condition
.
This filter also returns 4 arguments:
$show
- boolean to determine whether or not to show the content within the conditional placeholder.$condition
- the conditional placeholder that has been matched.$full_match
- the full match, including the content and opening/closing conditional placeholder brackets.$EM_Event
- the event object being output.
if( !empty( $EM_Event->styles ) && preg_match('/^has_style_(.+)$/',$condition, $matches) ){
This is the most important line in our function. Firstly we check if the event has any styles, and if so the preg_match method runs a regex matching operation on the found condition 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.
If either of these conditions fail, that means that either we weren't dealing with a styles conditional placeholder or there were no styles to check anyway. In this case we can just return $show as is, because any custom conditional placeholders not handled by Events Manager will already default to not be shown.
If these conditions are met, then we proceed to check if the event has the style ID we're searching for _$matches[1]_
, which we've determined in our preg_match condition on line 3. If we do in fact have a matching style ID associated with this event, then we'll set $show
to true which will then show the contents within the conditional placeholder.
Using your custom conditional placeholder
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, like so:
[events_list]#_EVENTNAME {has_style_1}Has Style ID 1{/has_style_1}
[/events_list]
Further considerations
For further control, you can also use the em_output_conditional_placeholder
placeholder. Instead of the first parameter $show
, you get a $replacement
parameter which would be the final output of the conditional placeholder (which would usually either be blank or the contents within the conditional placeholder).
This may be useful if you want to do something further with the content provided, or alter such as alter it or provide specific content. For most purposes, em_event_output_show_condition
will suffice since you only need to determine whether the condition requested is true (i.e. show the content), or not (i.e. hide the content), and EM will do the rest.
Both filters are applied to every conditional placeholders, including the default placeholders that come with Events Manager.
Next Steps...
In our next tutorial, we're going to integrate our plugin with MultiSite Global Tables Mode, so we can make the best of searching for events on all blogs within a multisite network.