Disable Dashboard Access for All Regular Users

How to Disable Dashboard Access for All Regular Users

in WordPress Admin
Google+ LinkedIn

Have you ever wanted to prevent your visitors from accessing wp-admin or WordPress dashboard? There is a couple of reasons why you would want to disable access to wp-admin folder or disable dashboard access in general for regular users. Some websites allow commenting only for registered users but after the user is signed on he can access the WordPress dashboard. Granted, there’s not much he can do there if his role is Subscriber, but you might want to prevent access nonetheless. Just like we disabled WordPress admin bar for regular users. In this article you can find a way to disable dashboard access with one simple function.

This code snippet helps when you want users to signup but don’t wish to give them access to the WordPress dashboard. Anyone beneath an Editor will be automatically redirected to the homepage of your website.

Just place this WordPress code snippet inside of your theme’s functions.php. This function will check if a user is logged in, and if it does, then does he have capabilities equal to or higher than an Editor, if he has then he will be allowed to access WordPress dashboard otherwise if any of these requirements are not fulfilled then he will be redirected back to website homepage.

// disable dashboard access for all except admin
function wcs_disable_user_dashboard_access() {
    $file = basename( $_SERVER['PHP_SELF'] );
    if ( is_user_logged_in() && is_admin() && ! current_user_can( 'edit_posts' ) && $file != 'admin-ajax.php' ) {
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'init', 'wcs_disable_user_dashboard_access' );

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *