In my plugin I call the following:
$caps = array("read"=>true, "edit_posts"=>false,"edit_other_posts"=>false,"edit_published_posts"=>false);
add_role("employee", "Employee", $caps);
add_role("manager", "Manager", $caps);
I'm trying to allow a dropdown for user account creation so an admin can choose whether or not the created user is a manager or an employee.
wp_create_user( $_POST['ae_uname'], $_POST['ae_pass'], $_POST['ae_email'] ); //new user registration
$user = get_userdatabylogin($_POST['ae_uname']); //get user information by username
$user_role = new WP_User( $user->ID );
$user_role->remove_role( 'subscriber' ); //remove user role
$user_role->remove_role( 'employee' ); //remove user role
//if ($_POST['employee_role'] == "manager") {
$user_role->set_role( 'manager' ); //add new user
//} else {
// $user_role->add_role( 'employee' ); //add new user
//}
I verified all $_POST variables were submitting properly, but I went ahead and commented out the if statement just to be double sure. If statement or no, every user that is created is assigned the role of 'employee'.
I've tried $user_role->add_role( 'manager' );
and I tried $user_role->set_role( 'manager' );
Googling came up blank in terms of whether or not there was some 'default' role setting, and based off my add_role()
code, I don't see how I could have specified "employee" as a default.
How can I make sure that WordPress honors my role setting?
Edit:
Additionally I've tried
wp_update_user(
array(
'ID' => $user->ID,
'nickname' => $email_address,
'role' => 'manager'
)
);
Still no luck. If I remove the "employee" role, the users are created with blank roles.
i hope this is enough for inserting a new users
$userdata = array(
'user_login' => $_POST['login_name'],
'user_url' => 'http://www.kvcodes.com',
'user_pass' => NULL , // When creating an user, `user_pass` is expected.
'role' => 'employee');
$user_id = wp_insert_user( $userdata ) ;
// on success
if(! is_wp_error( $user_id ) ) {
echo "User created : ". $user_id;
}
Before using the user entered data, just apply the stripslashes and trim to clean it . also use esc_attr to clean it .