Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How would we restrict access to the WP admin area to all users except admins?
The users on our site have their own profile pages which do all the functions they need.

So admin should be off limits to all except admins.

How to do that?

share|improve this question
You mean there are 'front facing' user pages that don't require accessing anything located at yourdomain.com/wp-admin ? – curtismchale Mar 5 '11 at 15:37
Yes exactly. Is there something wrong with that? – Robin I Knight Mar 5 '11 at 15:42
Nope just clarifying. – curtismchale Mar 6 '11 at 2:01

6 Answers

You could write a plugin and hook into admin_init.

The codex actually gives an example with the feature you are looking for.

http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init#Example:_Access_control

share|improve this answer
1  
This is not a complete answer - it should be more then a hint and a link – bungeshea Jan 8 at 4:37

We can hook to the admin_init action and check if the user is an administrator by using the current_user_can() function to see if the current user can manage_options, which is something only an administrator can do.

This code, when pasted into your functions.php file, will display a message when a non-admin tries to access the dashboard:

function wpse_11244_restrict_admin() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( __('You are not allowed to access this part of the site') );
    }
}
add_action( 'admin_init', 'wpse_11244_restrict_admin', 1 );

If you prefer, you can provide better user experience by redirecting the user to the home page instead (the $_SERVER['PHP_SELF'] check is to ensure we don't redirect on an AJAX request):

function wpse_11244_restrict_admin() {
    if ( ! current_user_can( 'manage_options' )  && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
        wp_redirect( home_url() );
    }
}
add_action( 'admin_init', 'wpse_11244_restrict_admin', 1 );

If you want to redirect the user to their profile page, replace home_url() in the code above with the link.

share|improve this answer

Try the Adminimize plugin.
You can lock things down pretty well with that.

You could also try setting access up through htaccess file

share|improve this answer
1  
+1 for Adminimize. That's a beast of a plugin. Coupled with some custom role manager plugin, it simply rocks. (If I'm excused a personal opinion) :::: As a last note: please give more information than only links, it makes the Answer more complete. – brasofilo Oct 20 '12 at 5:02
function wpse_11244_restrict_admin() {
    if (!current_user_can('update_core')) {
        wp_die(__('You are not allowed to access this part of the site'));
    }
}

add_action('admin_init', 'wpse_11244_restrict_admin', 1);
share|improve this answer
4  
'administrator' is not a capability. It works just to maintain backwards compatibility. Check for 'update_core' or other admin capabilities. – toscho May 10 '12 at 16:10

I would use WP Frontend and set it for everybody expect admins.

share|improve this answer
Please provide more information then a link - it makes an answer more complete – bungeshea Jan 8 at 4:42

Put these lines in your functions.php

function baw_no_admin_access()
{
 if( !current_user_can( 'administrator' ) ) {
     wp_redirect( home_url() );
     die();
  }
}
add_action( 'admin_init', 'baw_no_admin_access', 1 );
share|improve this answer
2  
like @toscho said 'administrator' is not a capability, instead use admin capabilities like 'update_core' – Peter Dec 27 '12 at 9:33
Also, please explain what your code does, and how it works – bungeshea Jan 8 at 4:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.