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.


  1. It’s cool and does what it says it will do, BUT you need to remove the capitalization from “Administrator” otherwise it doesn’t work on version 3.3. I don’t know about other versions. This fact is confirmed by the documentation from

  2. Works like a charm (WordPress 3.4 here).
    Only change I made is “administrator” (no caps) into “level_2” so subscribers/contributers have no access but others have.

    1. Hi, the code was buggy, and I just fixed it. If something wrong still happens to you, you can use FTP to access to your file, and then remove those lines of code.

  3. it works , but
    1- it break the add new post from front end plug in , it gives me error 404

    2- can you give me code to hid the bar of dashboard totally

    any help ,

Leave a Reply