Skip to content

Instantly share code, notes, and snippets.

@nikolov-tmw
Created February 4, 2014 12:25
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 nikolov-tmw/8802700 to your computer and use it in GitHub Desktop.
Save nikolov-tmw/8802700 to your computer and use it in GitHub Desktop.
Simple plugin that requires users to be logged-in to see any posts from a "projects" custom post type.
<?php
/**
* Plugin Name: Project Access Control
* Description: When a post of post type "project" is displayed and the current user is not logged-in, they are redirected to the login form.
* Version: 1
* Author: nikolov.tmw
* Author URI: http://paiyakdev.com/
* License: GPL2
*/
/*
Copyright 2013 Nikola Nikolov (email: nikolov.tmw@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function proj_restr_check_auth() {
// If we're viewing a "project" and the current user is not logged in
if ( 'project' == get_post_type() && ! is_user_logged_in() ) {
// Redirect the user to the login page.
// They will come back to the current page on successful login
auth_redirect();
}
}
add_action( 'template_redirect', 'proj_restr_check_auth' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment