Home / template / Restrict user access to specific templates
  • How to 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.

    ( click code to copy )

    wordpress snippet : PHP

    /* Template Name: Restricted to Authors only */
    if ( !current_user_can('author')) {

    ( WordPress codex functions, hooks, in this snippet. )

    template, current_user_can, user_can, get_template, get_template_part,
  • Sweet adapt Kevin 😉

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

  • 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.

    •  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.