Restrict WordPress Dashboard Access
5 January 2012 by BoiteAWeb
This only 5 lines snippet prevent any logged user but admins to visit the wordpress dashboard.
You can log-in on the wp-login.php page.

Just copy/paste it in your functions.php in your theme folder.
function baw_no_admin_access()
{
if( !current_user_can( 'administrator' ) ) {
wp_redirect( home_url() );
die();
}
}
add_action( 'admin_init', 'baw_no_admin_access', 1 );







Hi, thanks for this snippet.
I need to hide dashboard and toolbar to specified user role.
They should only be able to use a custom post type, this should edit inside my theme – not in dashboard style.
I tried your snippet, but how i can activate it?
Thanks for your support…
Marcel
Hello Marcel, you just have to copy/paste in your functions.php file from your theme folder, no other activation is required.
Now, if you want log in the admin area, go to /wp-login.php instead of /wp-admin/ of course.
See you
Hi! Today I tried your snippet, it didn’t work, I don’t know why. Once I refreshed the page I got a blank page with this message:
“Fatal error: Call to undefined function add_action() in /home/afbteam/public_html/wp-includes/functions.php on line 32″
Would you be so kind to explain me what did I do wrong?
Thank you in advance!
Hello Andrey
I don’t think that “/home/afbteam/public_html/wp-includes/” is your theme folder.
Find the good path, kind of “/home/afbteam/public_html/wp-content/themes/(theme name)/functions.php”
This file already exists and contains some theme functions.
See you