Skip Navigation

[Resolved] Add site to new user, multisite.

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 50 replies, has 2 voices.

Last updated by Shane 6 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#1134965

I am trying to: Hi, I have a function that auto add a site in my multisite network when register on the main page: looks like this: add_action( 'user_register', 'user_registration_create_site', 10, 1 );

function user_registration_create_site( $user_id ) {

$user_info = get_userdata( $user_id );
if ( isset( $user_info->user_login ) ) {
$main_site = 'weareyou.online'; // Add your main domain here
wpmu_create_blog( $main_site, '/'.$user_info->user_login.'/', $user_info->user_login, $user_id , array( 'public' => 1 ) );
}

}

I can't get it to work with user form... No sub sites is created. What can be the issue?

#1135097

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nicklas,

Thank you for contacting our support forum.

You should be able to use the hook below with your code to do this.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Please let me know if you understand how to get this done, if not i would be more than happy to guide you through this.

Thanks,
Shane

#1135118

Hi again and thank you for that answer,
it would be very kind if you could help me a little more:)

/Nicklas

#1135140

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nicklas,

Try this.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
      $user_info = get_userdata( $post_id );
if ( isset( $user_info->user_login ) ) {
$main_site = 'weareyou.online'; // Add your main domain here
wpmu_create_blog( $main_site, '/'.$user_info->user_login.'/', $user_info->user_login, $user_id , array( 'public' => 1 ) );
}
    }
}

Add it to your functions.php file and change the 12 to the ID of you form.

Thanks,
Shane

#1135159

Thanks!! It works almost exactly as I want. The page is created, now I just want the new user to be added to that page. Is that possible?:)

Think I can use add_user_to_blog(). But im not an PHP expert so if you have any ideas, please feel free to help me again:)

Thanks again!

#1135164

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nicklas,

You are absolutely correct.

Use this.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
      $user_info = get_userdata( $post_id );
if ( isset( $user_info->user_login ) ) {
$main_site = 'weareyou.online'; // Add your main domain here
$new_blog = wpmu_create_blog( $main_site, '/'.$user_info->user_login.'/', $user_info->user_login, $user_id , array( 'public' => 1 ) );
add_user_to_blog( $new_blog, $post_id, $user_info->roles );

}
    }
}

Please let me know if this helps.

Thanks,
Shane

#1135176

It did not help.. any ideas?

#1135183

Oh I didnt change the form ID, so it works perfect. Now I have one last question, the new user is now added to two pages. main site (weareyou.online) and weareyou.online/username. Can you delete the user from the main site? The new user should only be added to his subsite

#1135191

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nicklas,

Try this one now.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
      $user_info = get_userdata( $post_id );
if ( isset( $user_info->user_login ) ) {
$main_site = 'weareyou.online'; // Add your main domain here
$new_blog = wpmu_create_blog( $main_site, '/'.$user_info->user_login.'/', $user_info->user_login, $user_id , array( 'public' => 1 ) );
add_user_to_blog( $new_blog, $post_id, $user_info->roles );
 wp_delete_user( $post_id); 
}
    }
}
#1135663

Now it works, but when I try to login on the new site I get "the page redirects incorrectly" error. When I create a page manually, I can log in.

#1135720

I found the problem. But now my client wants the new pages that are created to have URL: hidden link.
And if a user has the same name, add a number: hidden link. Do you think you have a solution for that?:)

#1136077

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nicklas,

I'm happy i'm able to help so far.

So this number, should it be a random number ? Or do they want to add the user ID to the end?

Please let me know.

Thanks,
Shane

#1136203

I'm so grateful for your help!
I think if an existing user has url: /john-anderson. Then the next person with the same name get /john-anderson1 and the next /john-anderson2. But maybe it's hard? User ID may be a better solution.

Now another issue has emerged as well. My clien wants to be able to approve new users before they are registered? Is that possible?

,Nicklas

#1136228

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nicklas,

The ID would be easier, However the approval of the user would be much too complex for us to solve here in the forum.

What I would recommend is that you contact one of the registered contractors on our site for them to assist you with this.

https://toolset.com/contractors/

To add the user ID, just replace this link below.

$new_blog = wpmu_create_blog( $main_site, '/'.$user_info->user_login.'/', $user_info->user_login, $user_id , array( 'public' => 1 ) );

With

$new_blog = wpmu_create_blog( $main_site, '/'.$user_info->user_login.'/', $user_info->user_login.''.$user_id, $user_id , array( 'public' => 1 ) );

Please let me know if this helps.
Thanks,
Shane

#1139188

Thanks!
Is it possible to auto set a user role to the new user? I have a custom role called "athlete"