Skip to main content

Modifying Placeholder Default Information

Some placeholders, such as #_BOOKINGFORM and #_LOCATIONMAP, are printed by a template file you can override. Most are short values produced in code. For those, a filter lets you change the result without touching the plugin.

It is the same filter used to create a placeholder. The difference is that the value arriving in your callback is what Events Manager was about to print, and you change it.

What you will build​

A change to #_LOCATIONNAME, so that it prints the location's town after its name ("Harbor Hall, Maple Bay").

Before you start

You need Events Manager 7.5 or later and somewhere to put PHP (Where to add PHP).

Steps​

  1. Pick the filter for the object the placeholder belongs to: em_event_output_placeholder, em_location_output_placeholder, em_booking_output_placeholder, em_category_output_placeholder or em_tag_output_placeholder.

  2. Add a callback that checks for your placeholder and changes the replacement.

  3. View a page that uses the placeholder.

Add the town to #_LOCATIONNAME
add_filter( 'em_location_output_placeholder', 'my_em_location_name_with_town', 10, 4 );
function my_em_location_name_with_town( $replace, $EM_Location, $full_result, $target ) {
if ( $full_result === '#_LOCATIONNAME' && $target === 'html' && ! empty( $EM_Location->location_town ) ) {
$replace .= ', ' . esc_html( $EM_Location->location_town );
}
return $replace;
}

How it works​

The filter passes five arguments, and the callback uses four:

ArgumentContains
$replaceWhat Events Manager is about to print for this placeholder
$EM_LocationThe object being formatted (an EM_Event on the event filter, and so on)
$full_resultThe placeholder as written in the format, including any arguments in braces
$targetThe kind of output: html, email, rss, ical, map or raw
$placeholder_attsThe placeholder split into its name and arguments

Three points to keep in mind:

  • Return $replace for every placeholder that isn't yours. The filter runs for all of them.
  • Escape what you add. The value is inserted into the page as it is.
  • Check $target when your change is HTML. The same placeholder is used in emails, feeds and calendar files, where markup may not be wanted. The example changes web pages only.

Location placeholders used inside an event format, such as #_LOCATIONNAME in an events list, are passed to the location object, so the location filter covers them too.

Use a template file for larger output​

When the replacement is more than a line of HTML, keep it in a template file and load it from the filter. em_locate_template() looks in the same override folders as Events Manager's own templates and, with its second argument set to true, includes the file with the variables you pass.

Load a template for #_LOCATIONNAME
add_filter( 'em_location_output_placeholder', 'my_em_location_name_template', 10, 4 );
function my_em_location_name_template( $replace, $EM_Location, $full_result, $target ) {
if ( $full_result === '#_LOCATIONNAME' && $target === 'html' ) {
ob_start();
$found = em_locate_template( 'placeholders/locationname.php', true, array( 'EM_Location' => $EM_Location ) );
$output = ob_get_clean();
if ( $found ) {
$replace = $output;
}
}
return $replace;
}
wp-content/plugin-templates/events-manager/placeholders/locationname.php
<?php /* @var EM_Location $EM_Location */ ?>
<span class="my-location-name">
<?php echo esc_html( $EM_Location->location_name ); ?>
<small><?php echo esc_html( $EM_Location->location_town ); ?></small>
</span>

If the file doesn't exist, em_locate_template() returns an empty value and the original replacement is kept.

Test it​

  1. View your events list, or any page whose format contains #_LOCATIONNAME. The town follows the location name.
  2. Make a test booking and check the confirmation email. If its template uses #_LOCATIONNAME, the name there is unchanged, because the target is email.