How to add user to blog?
You can use add_user_to_blog
function to do that. After Codex, it:
Grants the specified user permissions of the specified role on the specified blog.
And you use it like so:
add_user_to_blog($blog_id, $user_id, $role);
How to do it automatically?
You want to do this automatically, when new site is created, so you’ll have to write some code and use wpmu_new_blog
hook.
Here’s the list of it’s params:
- $blog_id
(int) (optional) Blog ID of the created blog. - $user_id
(int) (required) User ID of the user creating the blog. - $domain
(string) (optional) Domain used for the new blog. - $path
(string) (optional) Path to the new blog. - $site_id
(int) (optional) Site ID. Only relevant on multi-network installs. - $meta
(array) (optional) Meta data. Used to set initial site options.
So in your case you can use it like so:
function add_users_to_new_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
// get users you want to add to new blog
$users_to_add = new WP_User_Query( array( 'blog_id' => 1 ) ); // <-- change that 1 to proper blog id
foreach ( $users_to_add->get_results() as $user ) {
add_user_to_blog( $blog_id, $user->ID, 'administrator' ); // <-- change administrator to any role you need
}
}
add_action( 'wpmu_new_blog', 'add_users_to_new_blog', 10, 6 );