Limit Access to the WordPress Dashboard

Lately I am working on a site where user would register, log in and do most of the work from the front-end of the site. To implement such functionality, the very first thing that I had to do is to stop users from accessing the dashboard or back-end of WordPress. So, if you are also working on such project, most probably you would need this. So, keep reading.

First of all, there are so many ways to limit the user's access to the dashboard. Using plugin could be one of the easiest way to do that. However, I personally prefer to use snippets since this code was used on a very specific site. Let's dive into it.

function restrict_dashboard_access() {
   if (is_admin() && !current_user_can('administrator')) {
   exit; } }

This is fairly simple and easy to understand. All I am doing here is, creating a function and checking two condition at the same time. First, if the user is about to log-in to the admin panel or not and then secondly, if the user doesn't have "administrator" role, the user should be re-directed to the home page. So, technically only users with administrative privileges would be allowed to log-in to the admin panel. This is as simple as it can get.

Now, if you don't want users to be redirect to the home page, you can change it with any given url as parameter. Here is an example.


At this point, all you need to do is to copy and paste the snippet I mentioned above on your theme's functions.php file and update it.


Commenting is disabled.