public
Last active

Allow users with the role editor to add users to WordPress -- but only subscribers.

  • Download Gist
editor-add-roles.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<?php
/*
Plugin Name: Editors Add Users
Description: Allow editors to add user roles
Author: Christopher Davis
Author URI: http://www.christopherguitar.me
License: GPL2
*/
 
register_activation_hook( __FILE__, 'wpse42003_activation' );
function wpse42003_activation()
{
foreach( array( 'editor', 'your_custome_role' ) as $r )
{
$role = get_role( $r );
if( $role )
$role->add_cap( 'create_users' );
}
}
 
register_deactivation_hook( __FILE__, 'wpse42003_deactivation' );
function wpse42003_deactivation()
{
foreach( array( 'editor', 'your_custome_role' ) as $r )
{
$role = get_role( $r );
if( $role )
$role->remove_cap( 'create_users' );
}
}
 
add_filter( 'editable_roles', 'wpse42003_filter_roles' );
function wpse42003_filter_roles( $roles )
{
$user = wp_get_current_user();
if( in_array( 'editor', $user->roles ) || in_array( 'your_custom_role', $user->roles ) )
{
$tmp = array_keys( $roles );
foreach( $tmp as $r )
{
if( 'subscriber' == $r ) continue;
unset( $roles[$r] );
}
}
return $roles;
}

Hi Christopher,

This is working well, but I wish we could allow Editors to EDIT the profiles as well. The thing is that if we provide them the 'edit_users' capabilities, they can modify admin profiles too... and we don't want that.

I can't find a way to sort this out. How would you hide non-subscribers profiles to the editors ? I posted this question on http://wordpress.org/support/topic/editors-can-only-edit-subscribers-profiles, but I thought you could eventually sort me out here.

Have a nice day !

WebMaestroFr, you can prevent the Editor from accessing the "admin" user.php and user-edit.php pages.

https://gist.github.com/leonardomdornelas/5746247

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.