Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top