Skip to main content

Overriding Event Page Content With Filters

Events Manager fills the pages you assign to it in Events then Settings then Pages, such as the events page, the locations page and My Bookings, by replacing the page's content when it is displayed. Three filters let you step into that process: one before Events Manager builds anything, one after, and one for the page title.

Use them when a change applies to the page as a whole. To change how each event in a list looks, edit the formats or override a template instead.

What you will build​

Two small examples: a notice added above the events list, and a "members only" message that replaces the events page for logged-out visitors.

Before you start

You need Events Manager 7.5 or later, an events page assigned in the settings, and somewhere to put PHP (Where to add PHP).

The three filters​

FilterArgumentsWhen it runsReturn
em_content_pre$content (an empty string), $page_content (the page's own text from the editor)Before Events Manager builds its outputA non-empty string to use as the content and skip the normal output, or $content unchanged to carry on
em_content$content (the finished HTML, inside a div with the id em-wrapper)After the output is builtThe HTML to print
em_content_page_title_pre$title (an empty string), $original_titleBefore Events Manager works out the page titleA non-empty string to use as the title, or $title unchanged

A fourth filter, em_content_page_title, receives the finished title and works like em_content.

All of them run only on pages assigned to Events Manager. Single event and location pages that use the post type's own address are ordinary WordPress posts and go through the_content instead.

Add to the page with em_content​

  1. Add the code below to your plugin.

  2. View your events page. The notice is above the list.

Add a notice above the events list
add_filter( 'em_content', 'my_em_content_notice' );
function my_em_content_notice( $content ) {
if ( function_exists( 'em_is_events_page' ) && em_is_events_page() ) {
$notice = '<p class="my-events-notice">' . esc_html__( 'Doors open 30 minutes before each event.', 'my-plugin' ) . '</p>';
$content = $notice . $content;
}
return $content;
}

em_is_events_page() is one of several conditional template tags. Others include em_is_event_page(), em_is_locations_page(), em_is_calendar_day_page() and em_is_my_bookings_page(). Always return $content, changed or not.

Replace the page with em_content_pre​

Show the events page to logged-in visitors only
add_filter( 'em_content_pre', 'my_em_members_only_events' );
function my_em_members_only_events( $content ) {
if ( function_exists( 'em_is_events_page' ) && em_is_events_page() && ! is_user_logged_in() ) {
return '<p>' . sprintf(
/* translators: %s: login URL */
wp_kses_post( __( 'Please <a href="%s">log in</a> to see our events.', 'my-plugin' ) ),
esc_url( wp_login_url( get_permalink() ) )
) . '</p>';
}
return $content;
}

Because the filter runs first, Events Manager does no work to build a list that would be thrown away. This hides the events page only. Event pages, feeds and other lists are not affected, so don't rely on it alone for access control.

Change the title​

Change the events page title for logged-out visitors
add_filter( 'em_content_page_title_pre', 'my_em_members_only_title' );
function my_em_members_only_title( $title ) {
if ( function_exists( 'em_is_events_page' ) && em_is_events_page() && ! is_user_logged_in() ) {
return esc_html__( 'Members only', 'my-plugin' );
}
return $title;
}

Test it​

  1. View the events page while logged in. The list appears with the notice from the first example above it.
  2. Open the page in a private window. The login message replaces the list and the title reads "Members only".
  3. View a normal WordPress page. Nothing has changed there.