wordpress snippets last updated   : October 6, 2014
we now have 641 snippets

Restrict user access to specific templates

wordpress snippet

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.

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

Did you like this post? If so please share it with your friends. Thanks!

  • http://www.squareonemd.co.uk Elliott the web design guy

    Sweet adapt Kevin ;-)

    • http://wpsnipp.com Kevin Chard

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

  • http://www.cliffpaulick.com Cliff Paulick

    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.

    • http://wpsnipp.com Kevin Chard

       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.