0

I have added some extra capabilities to the existing 'editor' role in wordpress with:

function add_uedit_caps() {
    $role = get_role('editor');
    $role->add_cap('create_users');
    $role->add_cap('list_users');
    $role->add_cap('add_users');
    $role->add_cap('edit_users');
}
add_action('admin_init', 'add_uedit_caps');

This piece works fine.

ISSUE:

The above code works to list and view all types of users by the 'editor'. But if a new user added by the 'editor', it takes up the 'new user default role' set up in wordpress (reference), ie the role set in settings > general. No role change option is available too in the user edit mode.

I want the 'editors' to list/view/add/edit only the users with the role 'author'. How can I achieve this through code (a method or class with action) and WITHOUT any plugin or tampering the core files?

1 Answer 1

1

In addition to the code piece in the question,

1. To display only the author roles in the user list page of editor:

add_action('pre_user_query','editors_edit_author_list');

function editors_edit_author_list($user_search) {
        $user = wp_get_current_user();
        if($user->ID != 1) {
            $user_meta=get_userdata($user->ID);
            //$user_roles= $user_meta->roles;
            global $wpdb;
            if(in_array('editor', $user_meta->roles))
            {
                $user_search->query_where = str_replace(
            'WHERE 1=1', 
            "WHERE 1=1 AND {$wpdb->users}.ID IN (
                SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
                    WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities'
                    AND {$wpdb->usermeta}.meta_value LIKE '%author%')", 
            $user_search->query_where
        );
            }
            else
                $user_search->query_where = str_replace('WHERE 1=1', "WHERE 1=1 AND {$wpdb->users}.ID<>1", $user_search->query_where);
        }
    }

2. Make editors only edit the authors:

add_filter('editable_roles', array($this, 'editor_editable_roles'));

function editor_editable_roles($roles)
    {
        $user = wp_get_current_user();
        $user_meta=get_userdata($user->ID);
        if(in_array('editor', $user_meta->roles)) {
            $tmp = array_keys($roles);
            foreach($tmp as $r) {
                if('author' == $r)
                    continue;
                unset($roles[$r]);
            }
        }
        return $roles;
    }

3. Change the users added by the editor to author role (from the 'new user default role'):

add_action( 'profile_update', 'process_editor_added_user', 10, 1 );
add_action( 'user_register', 'process_editor_added_user', 10, 1 );

function process_editor_added_user($user_id)
    {
        $user = wp_get_current_user();
        if($user->ID != $user_id)
        {
            $u = new WP_User($user_id);
            $u->remove_role('subscriber');
            $u->add_role('author');
        }
    }

There is an alternative to point #3 by hijacking the default new user role. I have not tested this:

add_filter('pre_option_default_role', function($default_role){
    return 'author';
});

That's seems to be all.

1
  • This still works great! Thanks!
    – Gavin
    Apr 17 at 13:38

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.

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