Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Network site, add users to all sites using the their same role WordPress

  • SOLVED

<strong>UPDATE</strong>

<strong>Automatically add existing users to new multisite blog using the same role as blog #1 for each individual user.</strong>


All I simply wish for is all existing users to be active on newly added multi sites with their existing user role on blog #1.

Please see this screenshot, this what my current multisite looks like... [[LINK href="http://i.imgur.com/aEeDGVt.png"]]http://i.imgur.com/aEeDGVt.png[[/LINK]]

And this is how I want it to look when I add new sites (new sites in red)... [[LINK href="http://i.imgur.com/SsSgHFx.png"]]http://i.imgur.com/SsSgHFx.png[[/LINK]]

Currently I'm having to do this manually using sql queries. I would like it to happened automatically every time I add a new site. And if I update someones user role on blog #1, for it to then change for them on every site.

Willing to add more funds if some one has a solution.



---------------------------


Hello,

I have a network website.

I have lots of users but I want all users to have access to all the sites.

But as I add new sites to the network, the users do not appear on it, and I have to do a database find/replace to allow.

It is a real pain in the ass adding new users to. When I add new users, there is no option to select what site they appear on!

Surely multisite user handling must not be this difficult to manage.


----


My question is how can make users work across all network sites?

Also there user role should not change. This is where it gets tricky.



Please see screenshot of network user admin... [[LINK href="http://i.imgur.com/1yG6bdO.png"]]http://i.imgur.com/1yG6bdO.png[[/LINK]]

This is what I see when I am having to add user site access, this is what the structure of the table looks like...


<strong>Example 1 - Admin user role</strong>



meta_key: wp_capabilities
meta_value: a:1:{s:13:"administrator";b:1;}
meta_key: wp_user_level
meta_value: 10

user_id: 1
meta_key: wp_2_capabilities
meta_value: a:1:{s:13:"administrator";b:1;}
meta_key: wp_2_user_level
meta_value: 10

user_id: 1
meta_key: wp_3_capabilities
meta_value: a:1:{s:13:"administrator";b:1;}
meta_key: wp_3_user_level
meta_value: 10




<strong>Example 2 - Press user role</strong>



meta_key: wp_capabilities
meta_value: a:1:{s:5:"press";b:1;}
meta_key: wp_user_level
meta_value: 0

user_id: 1
meta_key: wp_2_capabilities
meta_value: a:1:{s:5:"press";b:1;}
meta_key: wp_2_user_level
meta_value: 0

user_id: 1
meta_key: wp_3_capabilities
meta_value: a:1:{s:5:"press";b:1;}
meta_key: wp_3_user_level
meta_value: 0





<strong>Example 3 - Editor user role</strong>



meta_key: wp_capabilities
meta_value: a:1:{s:6:"editor";b:1;}
meta_key: wp_user_level
meta_value: 7

user_id: 1
meta_key: wp_2_capabilities
meta_value: a:1:{s:6:"editor";b:1;}
meta_key: wp_2_user_level
meta_value: 7

user_id: 1
meta_key: wp_3_capabilities
meta_value: a:1:{s:6:"editor";b:1;}
meta_key: wp_3_user_level
meta_value: 7




I basically have to add rows incrementally every time I add a new site. I do this via a laborious sql query. And when adding individual users I have to do the same. Its just so painful.


Is there not a function or some other way of doing this?

This question is open with the price at the moment, as I am personally still in the dark about what route to go down.


Thanks

Answers (2)

2013-04-04

idt answers:

Hi Josh,

I also use http://wordpress.org/extend/plugins/multisite-user-management/

What you do so that existing users will be copied to your sites where you want them copied is you go to your Network Admin Dashboard> Settings > Network Settings. Below the page, under Multisite User Management, you set the default role you want to be assigned to new users for each of your sites where you want them copied when they register. Then Save. That should also copy the existing user after you save it and at the same time copy every new users to sites that have default roles set.

Hope that helps.

idt


Josh Cranwell comments:

Hello, I just tried this plugin.

And now all my users are on my latest added site using with the same role.

I have over 6 different users roles assigned my user list. But on the latest added site - they are now all using the same role.

I need it so the user roles stay the same as the previous sites. Not to change to a default role.


idt comments:

Would combining http://wordpress.org/extend/plugins/multisite-user-management/ and http://wordpress.org/extend/plugins/user-role-editor/ work for you?

So you use multisite user management plugin so they are copied then user user role editor to change the roles.

Thanks,
idt


Josh Cranwell comments:

Hello

I dont need to change the roles. I am happy with the current roles.

Also I my custom role types exist across my entire network/sites.

All I simply wish for all existing users to be active on newly added multi sites with their existing user role on blog #1.

Please see this screenshot, this what my current multisite looks like... [[LINK href="http://i.imgur.com/aEeDGVt.png"]]http://i.imgur.com/aEeDGVt.png[[/LINK]]

And this is how I want it to look when I add new sites... [[LINK href="http://i.imgur.com/SsSgHFx.png"]]http://i.imgur.com/SsSgHFx.png[[/LINK]]

Currently I'm having to do this manually using sql queries. I would like it to happened automatically every time I add a new site. And if I update someones user role on blog #1, for it to then change for them on every site.

Thanks


idt comments:

Hi Josh,

Can you please try adding this to your theme's functions.php?

add_action('wp','add_current_user_to_blog',10);

/*add logged in user to current blog*/
function add_current_user_to_blog(){
if(!is_user_logged_in())
return false;

global $current_user,$blog_id;

$userRole = ($current_user->data->wp_capabilities);
$role = key($userRole);

if(!is_blog_user())//check for current membership
add_user_to_blog($blog_id, $role) //copy to current blog so roles work
}


What is does is when a logged in user visits a blog on your network, it checks whether he/she is a member of the current blog or not. If not, it adds them as a member to the current blog.

I used that before using http://wordpress.org/extend/plugins/multisite-user-management/.

More here: http://www.thinkinginwp.com/2010/06/automatically-adding-users-to-all-blog-on-your-wpmuwp-multisite-blog-network/


Josh Cranwell comments:

Hello, this seems to work in your want to set a default role.

It's not ideal but kind of works - but the problem is you can get the current user role because he is not a user on the site yet.

I tested this...


add_action('wp','add_current_user_to_blog',10);
function add_current_user_to_blog(){

if(!is_user_logged_in())
return false;

global $current_user,
$blog_id;

$user_Role = ($current_user->data->wp_capabilities);
$add_Role = key($user_Role);

if (!is_blog_user()) //check for current membership
add_user_to_blog($blog_id, $current_user->ID, 'press'); //copy to current blog so roles work

}


And it works.

I tried your code and it didnt work. I tried printing $current_user->data->wp_capabilities on the site when logged in with a where the user does not exist, and also on a site where the user does exist. Both times returned nothing.

I can use this to get the user role of the current site... $current_user->role[0]

But this is not use to be cause the user I am trying to add is not user on the site yet, and therefor there is no $current_user info to get.

So this idea is flawed unfortunately, unless u can get the role from blog #1 for the current_user.

I need something like a plugin to scan all users in blog #1 and assign add activate them on all blogs and new blogs using their same role. But I need it automatically happen when I add new users too.



idt comments:

What happens if you add switch_to_blog() so we can get the user role from the main blog. So it becomes:

add_action('wp','add_current_user_to_blog',10);

function add_current_user_to_blog(){



if(!is_user_logged_in())

return false;



global $current_user,

$blog_id;


switch_to_blog(1); //get role from main blog
$user_Role = ($current_user->data->wp_capabilities);
$add_Role = key($user_Role);
restore_current_blog();


if (!is_blog_user()) //check for current membership

add_user_to_blog($blog_id, $current_user->ID, '$add_Role); //copy to current blog so roles work



}


idt comments:

Oops sorry, typo on the code above. Please try this instead:

add_action('wp','add_current_user_to_blog',10);

function add_current_user_to_blog(){

if(!is_user_logged_in())
return false;

global $current_user, $blog_id;

switch_to_blog(1); //get role from main blog
$user_Role = ($current_user->data->wp_capabilities);
$add_Role = key($user_Role);
restore_current_blog();
if (!is_blog_user()) //check for current membership
add_user_to_blog($blog_id, $current_user->ID, $add_Role); //copy to current blog so roles work
}


Josh Cranwell comments:

Hello, thanks for you help.

This was my final code...


// AUTO ADD USERS
add_action('wp','add_current_user_to_blog',10);
function add_current_user_to_blog(){

if(!is_user_logged_in())
return false;

global $current_user,
$blog_id;

switch_to_blog(1); //get role from main blog
$get_role = ($current_user->h0ndamcpr13_capabilities);
$add_role = key($get_role);
restore_current_blog();

if (!is_blog_user()) //check for current membership
add_user_to_blog($blog_id, $current_user->ID, $add_role); //copy to current blog so roles work

}


Seemed to do the trick :-)

I will message you if I have any problems but have tested it and it works good.

Many Thanks
Josh

2013-04-02

webGP answers:

Hello!

Try to install this plugin: http://wordpress.org/extend/plugins/multisite-user-management/


Josh Cranwell comments:

Thanks. I've just installed the plugin.

But my network user admin only shows the old sites still... http://i.imgur.com/1yG6bdO.png

This does not add any new network sites I add onto existing users?

I'm not exactly sure what this plugin is doing? Have you used it before?


Thanks


webGP comments:

From plugin FAQ:
"Existing users will receive the default role. If you change the default role, all of your users with the old default role will receive the new default role."


Josh Cranwell comments:

Hmmm. This is confusing me a little, maybe because my multi site will not work like that.

I've got about 6 different user roles - so there is no such thing as a default role on my website.

For example I got...

3 administrators
8 individuals
4 editors
22 team
146 press


I need roles to do specific things. I cant have a default role overriding my specially defined roles.

I can't see this plugin helping me much?

I need just need users to appear on all the sites with there specified role to be the same through out.


webGP comments:

I will prepare a plugin for you, but could you tell me how you assign role for new user who doesn't any permission for all your sites?


webGP comments:

I wrote a plugin, you should create a php file (e.g. userRoles.php) with this content (http://pastebin.com/R43TZNM3), upload and choose network activate. There is one problem, I don't know how you store default user roles, as you can see in my code I have $default_role variable, that should keep the actual default user role, but I have no idea hot to get it.


Josh Cranwell comments:

There is no default role as such. Registrations are disabled by the public, but if I was going to use a default role, it would have to be 'press'

What I have been doing up to now is uploading all my users again via a CSV.

This plugin... http://wordpress.org/extend/plugins/csv-importer/

And every time I add a new network site, I have basically re-uploaded all the users again, but with a two extra cells which add the extra site privilege.

This is what my CSV looks like... pastelink.me/dl/d7acf7


And I am going to also create a user registration form using gravity forms - but have a hidden field that defines the user role.

I have got to leave my computer so I will check out your plugin tomorrow.

Many Thanks
Josh


Josh Cranwell comments:

Hello,

Thank you from creating me a plugin. Sorry I have been doing photoshop work all day :/

Anyway - I made a backup of my database before using any multisite plugins :-)

Ok I first initially installed Multisite User Management to test how it worked. I setup the default user role to 'press' on my new multisite and clicked save changes. YES! All my users were now activated on my new multisite - BUT they all had the same user role 'press'. Which is not good. I need all my users to have the same role as they do in blog #1.

Ok, so I dropped my database and re-uploaded the backup.

I then proceeded to try your plugin. I checked my users on the new multi site and they were empty. I first changed the default role to 'press' on line 31 + 48.

I then activated your plugin - then I went to check the users on the new multisite. And no users were appearing on old site?

So I think your plugin is not working. Unless I need to do anything else.


Thanks for you help so far.

Josh



Josh Cranwell comments:

All I simply wish for all existing users to be active on newly added multi sites with their existing user role on blog #1.

Please see this screenshot, this what my current multisite looks like... [[LINK href="http://i.imgur.com/aEeDGVt.png"]]http://i.imgur.com/aEeDGVt.png[[/LINK]]

And this is how I want it to look when I add new sites... [[LINK href="http://i.imgur.com/SsSgHFx.png"]]http://i.imgur.com/SsSgHFx.png[[/LINK]]

Currently I'm having to do this manually using sql queries. I would like it to happened automatically every time I add a new site. And if I update someones user role on blog #1, for it to then change for them on every site.

Thanks