Deluxe Blog Tips About Projects

Howto Prevent Admin Panel Access For Logged In Users In WordPress

Sometimes you don't want normal users access WordPress admin panel, all users' actions can be done in the front-end. This article will show you a small tip for preventing admin panel access for logged in users in WordPress.

Howto Prevent Admin Panel Access For Logged In Users In WordPress

Paste the following code into your functions.php file:

function prevent_admin_access()
{
    if ( false !== strpos( strtolower( $_SERVER['REQUEST_URI'] ), '/wp-admin' ) && !current_user_can( 'administrator' ) )
        wp_redirect( home_url() );
}

add_action( 'init', 'prevent_admin_access', 0 );

The script will check the request URI, if it contains /wp-admin, and users don't have administrator role, then we redirect them to the homepage.

The default user role for admin panel access is administrator, you can change it into any role (editor, author, ...) or any capability (edit_dashboard, edit_posts, ...). If users have the given role (or higher) or they have given capability, then they can access to WordPress Dashboard.

Preventing admin panel access is often used in many themes, where all users' actions can be done in the front-end, for example in Instant Q & A theme.