Skip to content

Instantly share code, notes, and snippets.

@jo-snips
Forked from jkudish/logged-in-events.php
Created March 9, 2012 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jo-snips/2004702 to your computer and use it in GitHub Desktop.
Save jo-snips/2004702 to your computer and use it in GitHub Desktop.
The Events Calendar: Prevent non logged in users from seeing The Events Calendar
<?php
/*
Plugin Name: Logged in only events calendar
Plugin URI: http://tri.be/support/forums/topic/calendar-view/#post-14059
Description: Prevents non-loggedin users from seing The Events Calendar. Requires The Events Calendar by Modern Tribe Inc
Version: 0.1
Author: Joachim Kudish
Author URI: http://jkudish.com/
License: GPLv2
*/
/**
* @package The Events Calendar
* @author Joachim Kudish @link http://jkudish.com
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Hooks into the parse_request WordPress hook and determines if the page being viewed is an Events Calendar page,
* if yes and the user isn't logged in, it redirects them to the login url
* once logged in they will be redirected back to the page they came from
*
* @author Joachim kudish <jkudish.com>
* @link http://tri.be/support/forums/topic/calendar-view/#post-14059
* @link http://kovshenin.com/2012/01/current-url-in-wordpress-3754/
* @param stdClass $wp the global $wp object
* @return null
*/
add_filter('parse_request', 'tribe_parse_request_for_non_logged_in_users');
function tribe_parse_request_for_non_logged_in_users($wp) {
if( class_exists('TribeEvents') && isset($wp->query_vars['post_type']) && $wp->query_vars['post_type'] == TribeEvents::POSTTYPE && !is_user_logged_in()) {
wp_redirect( wp_login_url( add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) ) ); exit;
}
}
@nikkoboy
Copy link

How would you add an exception for a specific taxonomy? Like for instance to prevent non logged in users from seeing The Events Calendar, except for events published under the category "public" (in this case events in the category "public" would be visible to both logged in and logged out users)?
Thanks!

@jo-snips
Copy link
Author

@nikkoboy I don't even know if this filter works anymore since it's 10 years old, but if it does you could try something like:

if( class_exists('TribeEvents') && isset($wp->query_vars['post_type']) && $wp->query_vars['post_type'] == TribeEvents::POSTTYPE && !$wp->query_vars['taxonomy'] == 'tribe_events_cat' && !$wp->query_vars['terms'] == 'public' && !is_user_logged_in()) {
wp_redirect( wp_login_url( add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) ) ); exit;
}

You might also check their knowledgebase, I just found this: https://theeventscalendar.com/knowledgebase/k/hide-events-of-a-category-on-the-calendar/

I hope that helps!

@nikkoboy
Copy link

@jo-snips thanks for replying to my question! And so diligently! I can testify that this filter still works flawlessly, as I've been using it for the past three years. I was trying this time to fine-tune it. I will try and implement the snippet of code you kindly shared, and if needs be, I'll liaise with the team at the event calendar. Anyway, I'll update this thread once I manage to have this work on my site. Could be useful to others.
Thanks again for your help

@jo-snips
Copy link
Author

Sounds good mate :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment