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.

<?php
function restrict_dashboard_access() {
   if (is_admin() && !current_user_can('administrator')) {
      wp_redirect(home_url());
   exit; } }
   add_action('init','restrict_dashboard_access');
?>

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.

wp_redirect('https://www.yourdomain.com/');

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.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.