As part of my work, I have to build many sites that require a sealed off section. A members area if you will. This was not a particularly challenging task, more annoying than anything. Now however I have developed a way that all the menu and content changes can be made through this one plugin and each time a menu or page needs to be restricted, I can use one line of code rather than typing up all the new code myself. Now ordinarily I would just put this in the functions.php file, but because I work on so many sites, I wanted to have one centralised version of my code and then whenever an update was needed, I would only ever have to update the one file rather than x number of them.
So now for the code. The function that you need to put in place is as follows.
function permit_access($user_types:Array,$type:String = "page", $template:String = "member-area.php")
The function itself is pretty straight forward. The $user_types variable is an array of string with the appropriate user type, whether it be admin, editor, etc. The $type variable is, at this stage, either a page or a menu, but could be expanded to work with other things. The last variable, $template, is simply to make sure that the code doesn’t run on any page it shouldn’t. I recently discovered due to my site design, that the menu could change on the public site and be replaced with the members area one. Whilst this isn’t a deal breaker or major issue, it’s something I didn’t want running.
There is only one flaw at this stage regarding the function and that is regarding the menus. The menu naming structure that needs to be in place for the menus to work is very specific and is used only mostly by myself. So if other people in the office wanted to use the plugin, they would have to follow the same structure but apart from that, the plugin has been in place for several weeks now on active sites and I have not received any complaints. The other issue is that the WordPress version required needs to be above 3.1 in order to work. However I have solved this by literally copying the needed code from a 3.1 version of WordPress and it all works fine
You can see the php file in full below to see how it works.
function permit_access($user_types,$type = "page",$template = "exhibitor-manual.php"){
$access = false;
foreach($user_types as $user_type){
if(get_current_user_role() == $user_type){
$access = true;
$u = $user_type;
break;
}
}
if(!is_page_template($template)){
$access = false;
}
if ($access){
if($type == "menu"){
print_menu($u);
}else{
print_page_access();
}
}else{
if($type == "menu"){
print_menu();
}else{
print_page_access_denied();
}
}
}
function print_menu($menu = "main"){
$menu .= "-nav";
wp_nav_menu( array( 'theme_location' => $menu ));
}
function print_page_access(){
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile;
}
function print_page_access_denied(){
?>
<h2>Insufficient Access</h2>
<p>You do not have access to visit this area of <?php bloginfo() ?>.</p>
<?php $url = home_url(); ?>
<p>Please <a href = <?php echo $url; ?> title = "Home Page">click here</a>
to be taken back to the home page<?php
if (!is_user_logged_in()){
?> or <a href = "<?php echo $url;?>/login/" title = "Login page">
click here</a> to log in to the site<?php
}
?>.</p>
<?php
}
function get_current_user_role () {
global $current_user;
get_currentuserinfo();
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
