X

How to Prevent Access to WP-Admin Redirect Subscribers to Home Page

Snippets by IsItWP

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.

Comments  Leave a Reply

  1. 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/’ );

  2. Thank you very much, it works but redirects me to another login.php panel, can’t it be home?

  3. Glenton Samuels June 21, 2012 at 11:37 am

    Doesn’t work for me.

  4. Wow thanks honey 🙂

    1.  No problem enjoy the snippet.

  5. Jason K. Seegmiller March 4, 2011 at 6:29 pm

    Does it matter where we add it?

    1. Just into the functions.php the location within that file does not matter.

  6. Tweets that mention WPSNIPP – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com November 21, 2010 at 2:43 pm

    […] 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 🙂 […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!