I'm trying to create a plugin where an admin (custom defined user role, not regular administrator) can access an options page, enter a specific username into a textbox, hit submit, and by doing so add a second user role to that user. I know that this code:
$user = new WP_User( null, 'username' );
$user->add_role( 'admin' );
will add the "admin" role to someone with the username "username." So in my plugin's settings page, I have a textbox:
Add an Admin: <input type="text" size="57" name="sscaur_options[txt_admin]" value="<?php echo $options['txt_admin']; ?>" />
and then this at the end:
function sscaur_add_admin($textbox) {
$options = get_option('sscaur_options');
$textbox = $options['txt_admin'];
$user = new WP_User( null, '{$textbox}' );
$user->add_role( 'admin' );
return $textbox;
}
I know that's totally wrong, but don't know how to begin to make it right.
I know that adding second user roles can be done with the User Role Editor plugin, but that's hardcoded to only give access to actual administrators, and I need my custom user roles to be able to add roles to specific users without being full administrators.