Over a million developers have joined DZone.

Restrict Admin Area To Only Admin Users

· Web Dev Zone

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

Lock
When you create a new user in the WordPress user manager they will be able to login to your site by going to the default WordPress login page /wp-login.php. Depending on the role you have assigned to this user they will see different options in the admin area.

Even if the user has the lowest level of role a Subscriber assigned to them they will be able to log into the WordPress admin area but all they will be able to do is edit their own profile. But if your not doing anything with this profile information then there is no need for them to login to edit the profile.

If you don't want other users to be able to log in to your WordPress admin area you can restrict access depending on a certain role.

In this example we are just going to allow admin users access to login to the admin area. We are going to use the WordPress action admin_init which will run when you access any admin page, from here we can check the access right of the user and redirect if we need to.

Copy the following code into your functions.php file and it will check if the user has admin rights, if they doesn't have admin rights then they will be redirected to the home page of the site.

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

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}