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").
You need Events Manager 7.5 or later and somewhere to put PHP (Where to add PHP).
Steps
-
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_placeholderorem_tag_output_placeholder. -
Add a callback that checks for your placeholder and changes the replacement.
-
View a page that uses the placeholder.
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:
| Argument | Contains |
|---|---|
$replace | What Events Manager is about to print for this placeholder |
$EM_Location | The object being formatted (an EM_Event on the event filter, and so on) |
$full_result | The placeholder as written in the format, including any arguments in braces |
$target | The kind of output: html, email, rss, ical, map or raw |
$placeholder_atts | The placeholder split into its name and arguments |
Three points to keep in mind:
- Return
$replacefor 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
$targetwhen 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.
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;
}
<?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
- View your events list, or any page whose format contains
#_LOCATIONNAME. The town follows the location name. - Make a test booking and check the confirmation email. If its template uses
#_LOCATIONNAME, the name there is unchanged, because the target isemail.