Events Manager conditional placeholders for custom attributes

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.

Share

21 thoughts on “Events Manager conditional placeholders for custom attributes

  1. Jason King

    That worked nicely. The Event Manager plugin doesn’t come with a way to say that an event’s date has not yet been announced. But my client still wanted to advertise the course on their website and start signing people up.

    First I set up an attribute called TBA (to be announced).

    Then set up two new functions:
    1) has_att_tba (so I can display a message)
    2) has_no_att_tba (so I can hide the date)

    Here’s the code I used to display a message in lists and single events:

    {has_att_tba}
    #_ATT{TBA}
    {/has_att_tba}
    {has_no_att_tba}
    #j #M #Y #@_{ — j M Y}
    {/has_no_att_tba}

    Reply
    1. rmckay Post author

      Excellent, well done. Events Manager doesn’t cater for all occasions right out of the box, but it certainly is flexible enough. I’ve been fighting with a few other plugins that don’t let you hook into them the way Events Manager does, and it makes a real difference.

      Reply
  2. Tony

    Thanks for the post, your instruction is far clearer then the official documentation. But I just can’t get it to work.

    I have this in the functions.php:

    function filterEventOutputCondition($replacement, $condition, $match, $EM_Event){
    if (is_object($EM_Event)) {
    switch ($condition) {
    // #_ATT{Premium}
    case 'has_att_premium':
    if (is_array($EM_Event->event_attributes) && !empty($EM_Event->event_attributes['Premium']))
    $replacement = preg_replace('/\{\/?has_att_premium\}/', '', $match);
    else
    $replacement = '';
    break;
    }
    }
    return $replacement;
    }
    add_filter('em_event_output_condition', 'filterEventOutputCondition', 10, 4);

    And I have this within the ‘Default event list format’:

    #_ATT{Premium}
    {has_att_premium}
    #_ATT{Premium}
    More
    {/has_att_premium}

    NB: the ‘hideMe’ class will be set to display:none; as I don’t want the new attibute display, it’s only purpose is to show/side the #_EVENTURL.

    The attribute does display when set, but the conditional statement doesn’t show when the attribute is set.

    Any thoughts?

    Reply
    1. rmckay Post author

      G’day Tony,

      I can’t spot a problem, looking at your code. When you say “the conditional statement doesn’t show”, what do you actually get in your output? Can you be sure that your function is actually being called?

      e.g. for a quick test, change the last line to “return ‘filterEventOutputCondition’;” and it should write that function name all over the event if the function is being called.

      Also, if you turn on debugging (edit your wp-config.php file and change WP_DEBUG to true), does it give you any errors? NB: you might find that experience somewhat frightening, some plugins throw lots of warnings and errors!

      Reply
  3. Tony

    Thanks for getting back to me.

    I mean that where it should output the code within the conditional statement if it were true and being called there is nothing being shown within the front end source code. So it may not actually be being called. It displays the attribute that I have included outside of the conditional statement (to test that the attribute is being set).

    Do you mean replace:
    return $replacement;
    with:
    return “filterEventOutputCondition”;
    If so nothing happens.

    I an new to EM so I might have missed an obvious setting somewhere?

    Regards debug.

    In backend I get:

    Notice: load_plugin_textdomain was called with an argument that is deprecated since version 2.7 with no alternative available. in ………../wp-includes/functions.php on line 3553

    Notice: has_cap was called with an argument that is deprecated since version 2.0! Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead. in ………../wp-includes/functions.php on line 3551

    In frontend I only get:

    Notice: load_plugin_textdomain was called with an argument that is deprecated since version 2.7 with no alternative available. in ……./wp-includes/functions.php on line 3553

    Reply
    1. rmckay Post author

      G’day Tony, the debug messages just mean that a plugin or theme you’re using is using old parameters when calling some WP functions; nothing to worry about there.

      Now, I suspect you might be putting this code in the wrong file. I copied your code exactly, and pasted it into the bottom of the functions.php file in my active theme — i.e.

      /wp-content/themes/my-theme/functions.php

      and it worked as advertised. What file are you putting this code into?

      Also, have you ticked yes on Enable event attributes? in the General Options?

      Reply
  4. Tony

    Thanks for getting back to me.

    It’s going at the bottom of my my themes function.php file:
    http://pastebin.com/2FncBeiM

    Might it conflict with something else?

    And yes ‘Enable event attributes’ is checked. The new attribute is being set as I have included the attribute outside of the conditional statement to check that it is being set.

    It would be so much easier if you could include php code within the Setting>Layout>Formatting for the plug in.

    Reply
  5. Tony

    I meant functions.php.

    I have just added it to the Twenty Eleven functions.php file and it it working so it is something on my theme. I will see if I can figure that one out….

    Reply
    1. Tony

      Doh!

      I think it was down to a missing ‘;’ in a previous function. All working now, thanks for the input, helped me look at the problem from a different angle.

      Reply
  6. Dan

    Hi there,

    I was just wondering if there is a way to use multiple attributes as in: has_att_this or has_att_that. I have a few different prices which work great using conditional placeholders individually but I also want to be able to get rid of all prices when the venue is closed some weeks.

    Any ideas?

    Reply
    1. Ross McKay Post author

      G’day Dan, you can create a conditional placeholder that tests for any condition, be it a simple one-attribute test or more complex multi-attribute logic. So if you want to test whether either one or another attribute has a value, you can make one placeholder that does both tests. But for the example you mention, you probably just need a placeholder that tests that the venue is not closed, and wrap that around the prices.

      cheers,
      Ross

      Reply
  7. Sam Blowes

    Im using the NL2BR function like so:
    {nl2br}#_EVENTEXCERPT{/nl2br}

    And im echoing the result to check it:

    case 'nl2br':
    $replacement = nl2br(preg_replace('/\{\/?nl2br\}/', '', $match));
    echo $replacement;
    break;

    And it reurns: #_EVENTEXCERPT

    What am I doing wrong?

    Reply
    1. Ross McKay Post author

      G’day Sam,

      The problem was that conditional placeholders are processed before “regular” placeholders, so #_EVENTEXCERPT wasn’t being replaced until after the {nl2br} replacement. I’ve changed the code above to process the contents of {nl2br} before calling nl2br(), so please update your code.

      cheers,
      Ross

      Reply
  8. Avery Z Chipka

    When I use the listed code it results in a error of

    “Parse error: syntax error, unexpected ‘{‘ in /home/crew505/public_html/dev/wp-content/themes/Venturing Crew 505/functions.php on line 11″

    line 11 includes the following code:
    if (is_object($EM_Event)) {

    Code is currently being placed in a child theme functions.php file.

    Please help

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>