Events Manager conditional placeholders for custom attributes

This post is more than 12 years old.

The Events Manager plugin for WordPress is pretty flexible, allowing you to easily add custom attributes to your event posts. It also has conditional placeholders that allow you to display or hide information conditionally. There is a nice tutorial on the plugin website showing you how to add your own conditional placeholders. Lets bring this all together with a conditional placeholder for a custom attribute.

Let’s say we want to add an attribute called Website, with a link to a website promoting the event. We could add it to the list of attributes and then add the following snippet to our Single Event Page format in the Events Manager settings:

<p>
  <strong>Website</strong><br/>
  <a href="http://#_ATT{Website}" target="_blank">#_ATT{Website}</a>
</p>

The problem is that it will always show this on the event page, even when our Website attribute is empty. To hide that content when the attribute is empty, we need to add a conditional placeholder for it — let’s call it has_att_website (for “has attribute Website”). A simple regular expression will take care of removing our conditional placeholder without removing the content it surrounds, as you’ll see in the code below.

Conditional placeholders can actually do more than just hide or show content, they can change it. A very simple use of this is to take a chunk of text with line breaks in it, and convert those line breaks to BR tags using the handy nl2br() PHP function. So let’s add that to our placeholder code too!

Here’s the code, which can be added to a plugin or to a theme’s functions.php file.

[edit: {nl2br} is processed before placeholders, so I have changed it to call $EM_Event->output() on its contents to pick up placeholders too.]

/**
* add some conditional output conditions for Events Manager
* @param string $replacement
* @param string $condition
* @param string $match
* @param object $EM_Event
* @return string
*/
function filterEventOutputCondition($replacement, $condition, $match, $EM_Event){
    if (is_object($EM_Event)) {

        switch ($condition) {

            // replace LF with HTML line breaks
            case 'nl2br':
                // remove conditional
                $replacement = preg_replace('/\{\/?nl2br\}/', '', $match);
                // process any placeholders and replace LF
                $replacement = nl2br($EM_Event->output($replacement));
                break;

            // #_ATT{Website}
            case 'has_att_website':
                if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['Website']))
                    $replacement = preg_replace('/\{\/?has_att_website\}/', '', $match);
                else
                    $replacement = '';
                break;

        }

    }

    return $replacement;
}

add_filter('em_event_output_condition', 'filterEventOutputCondition', 10, 4);

Now, we can change our Single Event Page format to make use of the conditional placeholder.

{has_att_website}
<p>
  <strong>Website</strong><br/>
  <a href="http://#_ATT{Website}" target="_blank">#_ATT{Website}</a>
</p>
{/has_att_website}

<p><strong>Something with line breaks</strong>
{nl2br}
#_ATT{Something with line breaks}
{/nl2br}
</p>

There, job is done if and when we want it.