Search blog

Monday, September 25, 2017

Disable WP Dashboard for Subscriber but Enable for Editor, Author and Other Custom Roles

Using the code below you can disable access to your WordPress Dashboard/Backend for selected roles and users belonging to that particular role. For example, if you want to restrict dashboard access to your guest users and users belonging to 'Subscriber' role then you may add the following code in your theme's function.php file or best way is to use the Code Snippet plugin.
 There are many added benefits, which you can read in the plugin's description page, when you use Code Snippet instead of directly inserting your code into your theme's function.php file.

 // Source: https://premium.wpmudev.org/blog/limit-access-to-your-wordpress-dashboard/  
 add_action( 'init', 'blockusers_init' );  
 function blockusers_init() {  
  if ( is_admin() && !current_user_can( 'administrator' )   
       && !current_user_can('editor')  
       && !current_user_can('author')  
       && !current_user_can('translator')  
       && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {  
      wp_redirect( home_url() );  
      exit;  
  }  
 }  

Note that I have also added a custom role called 'translator' in the above code.

Below is a screenshot of how I have added the code to my site by making use of the Code Snippet plugin.

Disable WP Dashboard for Subscriber but Enable for Editor, Author and Other Custom Roles
Click image to enlarge


No comments:

Post a Comment

Thank you for your Feedback!
www.evagabond.me

Top 5 Posts (Weekly)