4

I am trying to restrict access to wp-admin so that only administrators and editors are allowed. At the moment I am using this function:

function restrict_admin(){
    //if not administrator, kill WordPress execution and provide a message
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( __('You are not allowed to access this part of the site') );
    }
}
add_action( 'admin_init', 'restrict_admin', 1 );

But this does the job only for administrator; editors are not allowed to access this part of the site. What can I do?

4
  • What is different from the default behavior of Wordpress and what you want to implement exactly
    – Ramhound
    May 9, 2013 at 13:07
  • I want that editors could view information from wp-admin, but not having permission to change it.
    – user2358350
    May 9, 2013 at 13:11
  • Seems like the default permission system built-into Wordpress should handle a situation like this. There already is a tested and proven plug-in that allows you adjust the roles: wordpress.org/support/plugin/user-role-editor and a description of the default roles: en.support.wordpress.com/user-roles/#changing-user-roles just took my until now to realize you didn't write the code you posted. You can also add new roles: wordpress.org/extend/plugins/members and efine the exact permissins you want.
    – Ramhound
    May 9, 2013 at 13:32
  • I tested the code above and both Editors and Administrators are able to access the DashBoard whereas simple users are not.
    – user76229
    Jul 15, 2015 at 9:54

3 Answers 3

3

You're correct in that you should be checking for a capability. However, manage options is only given to administrators by default. You should check against a capability that both editors and administrators have such as delete_others_posts.

function restrict_admin(){
//if not administrator, kill WordPress execution and provide a message
    if ( ! current_user_can( 'delete_others_posts' ) ) {
        wp_die( __('You are not allowed to access this part of the site') );
    }
}
add_action( 'admin_init', 'restrict_admin', 1 );

See roles and capabilities from the codex.

0

You can also use this :

<?php if( current_user_can('editor') || current_user_can('administrator') ) {  ?> 
    // stuff here for admins or editors
<?php } ?>

For detailed reference check this another thread on stack.

Thanks

1
  • From the codex, "Passing role names to current_user_can() is discouraged as this is not guaranteed to work correctly" Mar 31, 2017 at 17:00
0

For me, since per Codex Passing role names to current_user_can() is discouraged as this is not guaranteed to work correctly (see #22624), best option is something like this.

$user = wp_get_current_user();
$allowed_roles = array('administrator','editor');

if(array_intersect($allowed_roles, $user->roles)){
    //user have $allowed_roles
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.