0

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.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
Works for a Living
  • 1,262
  • 2
  • 19
  • 44
  • What hook is calling `_add_admin`? Can you provide more context? – brasofilo Sep 06 '13 at 15:44
  • On its own it doesn't need a hook. Just putting this in functions.php will add the secondary user role: $user = new WP_User( null, 'username' ); $user->add_role( 'admin' ); – Works for a Living Sep 06 '13 at 18:12
  • Are you "creating a plugin" or "putting in functions.php"?. Use functions.php only for theme related stuff. There are few things that should not be wrapped in hooks, as we need to control where, when and how things happen. Right now, your sample code doesn't make any sense: it doesn't show a [reproducible example](http://sscce.org/). – brasofilo Sep 06 '13 at 18:31
  • I'm making a plugin. I know what functions.php is for. I also know that my sample code doesn't make any sense. That' why I'm here asking for help. – Works for a Living Sep 07 '13 at 05:56

0 Answers0