Skip to content

Instantly share code, notes, and snippets.

@joycse06
Forked from chrisguitarguy/editor-add-roles.php
Created April 19, 2014 17:21
Show Gist options
  • Save joycse06/11091047 to your computer and use it in GitHub Desktop.
Save joycse06/11091047 to your computer and use it in GitHub Desktop.
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment