Skip to content

Instantly share code, notes, and snippets.

@jkudish
Created January 25, 2012 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jkudish/1678347 to your computer and use it in GitHub Desktop.
Save jkudish/1678347 to your computer and use it in GitHub Desktop.
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!

@jkudish
Copy link
Author

jkudish commented Dec 14, 2021

Hi @nikkoboy

This code sample is 10 years old and I don't actively do any WordPress work at the moment, so I'm a bit out of the loop (pun intended). That being said, I believe $wp->query_vars['categories'] might contain the categories the post is in so you could compare it to that.

I'm not sure though and you will have to do a bit of trial and error. I recommend posting on the WordPress forums for additional help.

Good luck!

@nikkoboy
Copy link

@jkudish 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

@jkudish
Copy link
Author

jkudish commented Dec 15, 2021

👍

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