fbpx
How to Restrict User Access to WordPress Templates

How to Restrict User Access to WordPress Templates

With this snippet, you can create a WordPress page template with restricted access only to those users with the right role to access to it.

Instructions
You will need to create the include, in this case error.php – this include should contain the message you wish to show.
If your template-custom.php page, is not intended for a given user, then your error.php could say something like: “Sorry, you are not allowed to access this page!”. Or you could do a redirect in your error.php file.

If you create custom roles you can do the same ie. buyers, clients, etc.

Ad this to the top section of your template-custom-php file.

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

Related Links:
http://codex.wordpress.org/Roles_and_Capabilities

WordPress Snippet Info

Tested with Wordpress v. 4.9.5
Last Modified: 15.05.2018
Last Reviewed: 15.05.2018

How to Restrict User Access to WordPress Templates
4.2 (84%) 10 vote[s]
Please report broken snippets.
Report Broken Snippet

Recent Comments