Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The Events Calendar: Filter Bar: If use is not logged in, force them to use a Filter Bar filter in order to see any events in any Events Archive view (e.g. Month, List, Day, etc).
<?php
/**
* The Events Calendar: Filter Bar: If use is not logged in, force them to use
* a Filter Bar filter in order to see any events in any Events Archive view
* (e.g. Month, List, Day, etc).
*
* @link https://gist.github.com/cliffordp/49d5f296dcae2e6f13773d48d32fd274
*/
add_action( 'tribe_events_pre_get_posts', 'cliff_display_none_unless_filter_bar_in_use' );
function cliff_display_none_unless_filter_bar_in_use( $query ) {
if ( is_admin() ) {
return;
}
if ( is_user_logged_in() ) {
return;
}
if (
! class_exists( 'Tribe__Events__Main' )
|| ! class_exists( 'Tribe__Events__Filterbar__View' )
) {
return;
}
$archive_slug = Tribe__Events__Main::instance()->rewriteSlug;
if (
// if "events" does not match "/events", which is why we start the index at 1
$archive_slug !== substr( $_SERVER['REQUEST_URI'], 1, strlen( $archive_slug ) )
) {
return;
}
$probably_using_filter_bar = false;
// or if no query strings
if ( ! empty( $_GET ) ) {
foreach ( $_GET as $key => $value ) {
if ( substr( $key, 0, strlen( 'tribe_' ) ) ) {
$probably_using_filter_bar = true;
break;
}
}
}
if ( true === $probably_using_filter_bar ) {
return;
}
add_filter( 'posts_where', 'cliff_calendar_appear_empty', 100 );
add_filter( 'gettext', 'cliff_need_filter_bar_custom_no_events_message', 20, 3 );
}
// throw a wrench in things just to force displaying no results
function cliff_calendar_appear_empty( $where ) {
return $where . ' AND 1=0 ';
}
// adapted from https://theeventscalendar.com/knowledgebase/change-the-wording-of-any-bit-of-text-or-string/
function cliff_need_filter_bar_custom_no_events_message( $translation, $text, $domain ) {
$custom_text = array(
'There were no results found.' => 'Please choose a filter in order to display events.',
);
if (
0 === strpos( $domain, 'the-events-calendar' )
&& array_key_exists( $translation, $custom_text )
) {
$translation = $custom_text[ $translation ];
}
return $translation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment