I am very new to multisite and so I'm still confused about users management.
I am trying to create a example.com site for users for browsing music and a singer.example.com sub site where paid users can access manage their private paid contents, as well as SIGN UP for account.
So I have Main site with "theme 1" and subsite with "theme 2" where I am going to create a control panel and I have put this in THEME 2 so to create roles.
add_action('after_switch_theme', 'agent_init');
function agent_init () {
add_role( 'singer', 'Singer', array( 'read' => true, 'singer_cp' => true ));
add_role( 'producer', 'Producer', array( 'read' => true, 'developer_cp' => true ));
}
I have used wp_create_user in the subdomain site to create user successfully, but strange things start to happen when a admin delete the newly created user from the subdomain dashboard. And things goes very messy when I can login Main site and Subdomain site with **2 different logins*. Unless someone have a solution to this... i'm moving on to the solution below.
What I did After multiple failed attempts, and a lot of problems when i used wp_create_user in subsite.example.com, i've decided i should just switch blog ID so that the newly created user can access both main site and subsite with 1 login (cross fingers on cookie problems).
I've created a test page with the following codes included to create user.
if (isset($_GET['username'])) {
$username = $_GET['username'];
$password = $_GET['password'];
$email = $_GET['email'];
if(switch_to_blog(1)) {
echo 'Successfully Switched Blog!';
} else {
echo 'Cannot switch blog';
}
$newuser = wp_create_user($username, $password, $email);
if (is_wp_error($newuser)) {
echo 'Error Adding';
} else {
$user_id = wp_update_user(array('ID' => $newuser, 'role' => 'singer'));
if (is_wp_error($user_id)) {
echo 'Something happened when adding role';
} else {
echo 'Change role success!';
}
}
if (restore_current_blog())
echo 'Switch blog back!';
else
echo 'Failed switching blog back';
} else {
echo 'Welcome. U need variables set in order to add user.';
}
PROBLEM Now that i have the user added in the Main site and I can see it in the main site admin dashboard yay!
However the role is not there in the Dashboard User view, AND when I go to the mysql I can see the capability in usermeta being set like a:1:{s:6:"singer";b:1;} But if i login to the user and i call the following function, it returns 0 which means the role is not set.
function is_singer(){
global $current_user;
if (current_user_can('singer_cp'))
return 1;
else return 0;
}
Did I miss out something?
Action: add_role
NB: This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation
I've created the role with the permission so it shouldn't be a problem, so I have no idea what I have done wrong.