Reputation: 13598
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.
Upvotes: 0
Views: 1974
Reputation: 13598
I found my own solution.
When creating user by switch_to_blog();
It is necessary to in the future always switch blog when checking user capability and role by using the same switch_to_blog();
Example Function to check Capability
function is_singer(){
global $current_user;
switch_to_blog(1);
if (current_user_can('singer_cp')) {
restore_current_blog(); //!IMPORTANT - need to restore blog before return.
return 1;
} else {
restore_current_blog(); //!IMPORTANT - need to restore blog before return.
return 0;
}
Example how to use function to check capability
if(is_singer()) {
echo 'User is a Singer!';
} else {
echo 'User is not a Singer!';
}
Bonus - Adding User via Plugin (You only need to activate an deactivate once to create the roles). Using plugin allows this hook to be called only once since it will be added into the database after and no further actions needed.
1) Create a "initrole" folder inside /wp-content/plugins. e.g./wp-content/plugins/initrole
2) Create a "initrole.php" file in the folder you newly created.
3) add in the codes below to your plugin. you may add your own role name and customise capabilities
<?php
/*
Plugin Name: Custom Create User Role
*/
function add_roles_on_plugin_activation() {
remove_role('singer');
remove_role('producer');
add_role( 'singer', 'Singer', array( 'read' => true, 'singer_cp' => true ));
add_role( 'producer', 'Producer', array( 'read' => true, 'producer_cp' => true ));
}
register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );
?>
Just go to your plugins and Activate the newly created plugin. Viola! You are done!
Hope the above help people who are having problem adding users in multisite too.
Upvotes: 1