Do you want to redirect subscribers away from wp-admin to the home page of your WordPress website? While there is probably a plugin for it, we have create a quick code snippet that you can use to prevent access to wp-admin and redirect subscribers to home page.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
if ( is_user_logged_in() && is_admin() ) { global $current_user; get_currentuserinfo(); $user_info = get_userdata($current_user->ID); if ( $user_info->wp_user_level == 0 ) { header( 'Location: '.get_bloginfo('home').'/wp-login.php?redirect='.get_bloginfo('home').'/wp-admin/' ); } }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 9 best WordPress job board plugins and how to set up Google Analytics eCommerce tracking.
It doen’t work on my site
hi, my problem is connected to this issue.
My subscribers are not getting the temporary passwords or any email sent to them after registering to my website.
if you want it to redirect your homepage and not the login page us this:
header( ‘Location: ‘.get_bloginfo(‘home’).’/?redirect=’.get_bloginfo(‘home’).’/wp-admin/’ );
instead of
header( ‘Location: ‘.get_bloginfo(‘home’).’/wp-login.php?redirect=’.get_bloginfo(‘home’).’/wp-admin/’ );
Thank you very much, it works but redirects me to another login.php panel, can’t it be home?
Doesn’t work for me.
Wow thanks honey 🙂
 No problem enjoy the snippet.
Does it matter where we add it?
Just into the functions.php the location within that file does not matter.
[…] This post was mentioned on Twitter by wp_freak, WPSNIPP. WPSNIPP said: #wordpress Prevent access to wp-admin http://bit.ly/9CyZeF #blog please RT 🙂 […]