X

Restrict user access to specific templates

This is a slight modification of a great little snippet by Elliott Richmond I just changed "include" to "get_template_part". Create a new template file and drop in this snippet and you can restrict user access to this template. Don't forget to change the template name at the top. You will of course also need to create a template file called error.php or the other option is to use something like "wp_die('You don't have access.')" instead of loading a template.



Code Snippet:


<?php
/* Template Name: Restricted to Authors only */
if ( !current_user_can('author')) {
get_template_part('error');
exit(0);
}
?>

Comments  Leave a Reply

  1. What’s this for? Is this in the post-writing part of the back-end? Wouldn’t only Authors have access to template files already? Sorry if I’m missing something easy. Thanks.

    1.  Hi Cliff,  this would be on the front end for your theme but could be anything. I could setup a template that would let users post from the front end and use this to restrict access. Or this could simple display a list of users and I only let a specific people view it.

  2. Elliott the web design guy November 29, 2011 at 11:58 pm

    Sweet adapt Kevin 😉

    1. Thanks Elliott, get_temp is the encouraged method. But I can see a number of uses for this snippet.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!