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.

stop 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.

10 Comments

  1. 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 ,

    Reply
  2. how do i undo this?? I can’t get into my admin panel..I think I left something out of the code!!! help!

    Reply
    • 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.

      Reply
  3. 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.

    Reply
  4. 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 codex.wordpress.org.

    Reply
  5. Perfect for what I need! I wasn’t sure if this was even possible, but it works perfect

    Thanks :)

    Reply

Leave a Reply