Create Custom Event Information Pages
Part 10 of 11 in the Build an add-on series. Previous: Multisite global tables. Next: Custom permalinks.
Categories and tags have their own pages listing their events. This part gives styles the same: a page for each style, and an index of all styles, both served from your existing events page so they inherit its template and place in the site.
What you will build
Two views of the events page, chosen by a query variable:
| Query variable | Shows |
|---|---|
styles_page=1 | Every style as a heading, each followed by its next five events |
style_id=1 | The upcoming events with style 1, with the style's name as the page title |
At the end of this part they are reachable as /events/?styles_page=1 and /events/?style_id=1. Part 11 turns those into /events/styles/ and /events/style/1/.
Complete part 5, which provides the style search attribute. An events page must be assigned as Events page in Events then Settings then Pages then Event List/Archives.
Steps
-
Add these lines inside
my_em_styles_init():add_filter( 'em_content_pre', 'my_em_styles_content' );add_filter( 'em_content_page_title_pre', 'my_em_styles_page_title' ); -
Register the two query variables. Add this at file level, outside
my_em_styles_init():add_filter( 'query_vars', 'my_em_styles_query_vars' ); -
Add the functions below to the end of the file.
function my_em_styles_content( $content ) {
$styles = my_em_styles_get();
$style_id = absint( get_query_var( 'style_id' ) );
if ( $style_id && isset( $styles[ $style_id ] ) ) {
$content = '<p>' . sprintf( esc_html__( 'Upcoming events with the %s style', 'my-em-styles' ), '<strong>' . esc_html( $styles[ $style_id ] ) . '</strong>' ) . '</p>';
$content .= EM_Events::output( array( 'style' => $style_id, 'scope' => 'future' ) );
} elseif ( get_query_var( 'styles_page' ) ) {
foreach ( $styles as $id => $name ) {
$content .= '<h3>' . esc_html( $name ) . '</h3>';
$content .= EM_Events::output( array( 'style' => $id, 'scope' => 'future', 'limit' => 5, 'pagination' => 0 ) );
}
}
return $content;
}
function my_em_styles_page_title( $title ) {
$styles = my_em_styles_get();
$style_id = absint( get_query_var( 'style_id' ) );
if ( $style_id && isset( $styles[ $style_id ] ) ) {
return esc_html( $styles[ $style_id ] );
}
if ( get_query_var( 'styles_page' ) ) {
return esc_html__( 'Styles', 'my-em-styles' );
}
return $title;
}
function my_em_styles_query_vars( $vars ) {
$vars[] = 'style_id';
$vars[] = 'styles_page';
return $vars;
}
How it works
Events Manager fills its assigned pages (events, locations, categories, tags and the rest) by filtering the page's content. Before it builds anything, it passes an empty string through em_content_pre. If a callback returns something that is not empty, that is used as the page's content and the normal list, calendar or single event is skipped. Returning the value unchanged leaves the page alone.
em_content_page_title_pre does the same for the page title.
WordPress discards query variables it hasn't been told about. The query_vars filter registers style_id and styles_page, and after that get_query_var() returns them. Both callbacks read the variables that way and never touch $_GET directly.
Both callbacks check that the requested style exists and fall through to the normal events page if it doesn't. Style names are escaped on output. EM_Events::output() returns finished HTML, formatted with the event list format from your settings, so it is appended as it is.
The filters only run on pages that Events Manager manages, and these callbacks only act when one of the two query variables is present, so the rest of the site is unaffected.
To filter or wrap the normal content instead of replacing it, see Override page content.
Test it
- Open your events page with
?styles_page=1added to its address. The page title is "Styles" and each style is listed with its events. - Replace that with
?style_id=1. The title is the style's name and the list holds its upcoming events. - Try
?style_id=999. The normal events page appears.