Allow only admins access to admin area

WordPress comes with many features, even a user administration and comments are already included. To reduce spam and to keep the tone polite, registration is often required to post a comment, but maybe there are other reasons for the same. The problem with WordPress registration is that all users have access to the admin area afterwards. But there, normal users have simply not lost anything, moreover, access there is usually simply not desired. But the admin area can also be completely blocked with a simple snippet. Normal users who try to enter the admin area will be redirected to the website, only admins will get access to the WP-Admin. Very useful, especially to keep uninvited guests away.

Copy the following into the Functions.php of your theme:

function restrict_admin()
{
	if ( ! current_user_can( 'manage_options' ) ) {
                wp_redirect( site_url() );
                exit;
	}
}
add_action( 'admin_init', 'restrict_admin', 1 );

Lock WordPress Admin for normal users

The snippet is very simple and does not affect the performance. It only checks if the user is an admin after login. If this is the case, the user will be forwarded to the admin area of WordPress. If this is not the case, the snippet simply redirects to the normal website. This way no one, except the admin, has access to the administration. Although normal users can only change their profile anyway, this is usually completely unnecessary, since the data is not used or even needed anywhere. If you also use an Ajax widget for login (This one is exceptionally well done, by the way, because a password can be chosen already at registration), he also simply doesn't want users to move into the back areas of the WordPress backend anywhere. Anyway, I find the snippet very useful, I hope one or the other of you can use it as well.

About Christian

My name is Christian and I am co-founder of the platform fastWP. Here in the magazine I am responsible for the more "technical" topics but I like to write about SEO, which has been my passion for over 10 years now.

3 thoughts on “Admin-Bereich für normale User sperren”

  1. Hi, stupidly this also prevents access to admin-ajax.php - at least that's the way it is with me and for a plugin I need that...do you have an idea?

    Love, Andrea

  2. Hello, Christian,

    it works great. Thanks for that. Do you also have an idea how to hide the black bar on top (WordPress...)?

    Greeting
    Peter

    1. Hi Pezter, qucik and dirty because I don't know what you want to do and how you want to do it:

      #wpadminbar { display:none !important;}

      So copy this as CSS into the customizer.

Leave a Comment

Your email address will not be published. Required fields are marked *

en_GB