Create a Custom Placeholder for Event Formatting

This tutorial forms part of our series of tutorials for creating an custom add-on for Events Manager called “Styles”, where we will be able to select from a list of styles during event registration. In our previous tutorial, we added a custom field to the events search form.

Events Manager uses placeholders like #_EVENTNAME so that you can easily insert
snippets of html via the events settings page and change the way events, locations, categories and contact information are displayed. Moreover, you can use these in shortcodes, template tags, or when directly working with objects.

The great thing about placeholders is you can easily insert chunks of information with one word. Events Manager 3 onwards also has the ability for you to use your own custom placeholders or modify the way the default placeholders work. This could have many uses:

  • Create a shortcut placeholder you can use across many formats.
  • Adding a new placeholder for extra event information you have added to Events Manager
  • Use placeholders to do execute custom PHP code whilst displaying event information.
  • Change the formatting or behaviour of current placeholders

In this tutorial, we’re going to extend our custom Events Manager addon “Styles” plugin by adding a #_STYLES placeholder, which prints a comma separated list of the styles our event has.

So, here’s the code needed to do this:

add_filter('em_event_output_placeholder','my_em_styles_placeholders',1,3);
function my_em_styles_placeholders($replace, $EM_Event, $result){
	global $wp_query, $wp_rewrite;
	switch( $result ){
		case '#_STYLES':
			$replace = 'none';
			if( count($EM_Event->styles) > 0 ){
				$my_em_styles = (is_array(get_option('my_em_styles'))) ? get_option('my_em_styles'):array();
				$styles = array();
				foreach( $my_em_styles as $id => $name ){
					if(in_array($id, $EM_Event->styles)){
						$styles[] = $name;
					}
				}
				$replace = implode(', ', $styles);
			}
			break;
	}
	return $replace;
}

We start off by adding a filter for the ‘em_event_output_placeholder’ filter. This function will get called every time a placeholder is found and needs replacing. This filter returns three variables; $replace is the value of what this placeholder would be replaced with (and since this is not an EM placeholder, this would always be an empty string), $EM_Event is the EM_Event object, and $result is the placeholder that was matched.

We use $result to check for the use of our custom placeholder #_STYLES. If we find a match, we will firstly set the default replacement to ‘none’ so that if nothing is found a blank value isn’t used

Lets focus on this bit:

if( count($EM_Event->styles) > 0 ){
	$my_em_styles = (is_array(get_option('my_em_styles'))) ? get_option('my_em_styles'):array();
	$styles = array();
	foreach( $my_em_styles as $id => $name ){

Here we double-check the object is EM_Event and has more than one style, which we loaded on event instantiation.

If styles exist, we load the full list of styles from the wp_options database into $my_em_styles and loop through them to obtain the names of the style ids this event has. As we find the relevant styles, we add them to a $styles array and output that as a comma separated list.

As you can see, the possibilities are endless once you’ve hooked into the right placeholder name. We’ll leave that bit up to you….

In the following tutorial, we’re going to learn how to create a custom conditional placeholder for your events.