Auto create subsite at user registration

Hi there! I use New Blog Templates to create a subsite at a user’s request. Currently, my user gets created during a WooCommerce purchase. He or she is directed via the purchase confirmation email to a custom /wp-signup.php to create the subsite. I’d like to automate the creation of the subsite at user registration, to avoid having the user create their own. The subsite would be username.example.com. I know this would involve the two hooks, user_register and wpmu_create_blog(), but I don’t know how to connect them. Can anyone help? TIA.

  • Vinod Dalvi
    • WP Unicorn

    Hi Burlington,

    I hope you are well today and thank you for your question.

    You can try achieving this by using the following code as a plugin attached to this reply and network activating it.

    You have to change the example.com in the following code with your main domain. The site gets created after activating the user account.

    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 = 'example.com'; // Add your main domain here
    wpmu_create_blog( $user_info->user_login.$main_site, '/', $user_info->user_login, $user_id );
    }

    }

    Best Regards,

    Vinod Dalvi

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Thanks again for this extremely fast response (30 minutes!). The solution is 99% there. A site is created at user creation, but it is usernameexample.com rather than username.example.com. Not up on my php, but I think we need to add a ‘.’ after the user_login piece on the last line there. Will be excited to see this work. Thanks.

  • Vinod Dalvi
    • WP Unicorn

    Hi Burlington,

    Glad i could help you.

    To resolve the dot issue please try adding the dot . before your main domain in the provided code which i did in the following code for ‘.example.com’

    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 = '.example.com'; // Add your main domain here
    wpmu_create_blog( $user_info->user_login.$main_site, '/', $user_info->user_login, $user_id );
    }

    }

    Regards,

    Vinod Dalvi

  • Burlington Avenue
    • Site Builder, Child of Zeus

    I do have one small obstacle to overcome here. This solution creates the subsite at account creation perfectly (at successful payment), but I would also like to confirm this on the thank-you page and by email receipt of purchase. In my custom WooCommerce thank-you template, I have the following:

    <p><?php _e( "To set up your new subsite, please visit https://example.com/wp-signup.php and follow the instructions. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>

    I am wanting to instead provide the user with a link to the newly created subsite. Using the solution provided here, I’ve tried the following:

    <?php $user_info = get_userdata(); ?>
    <?php $cmdomain = 'https://' . echo $user_info->user_login . '.example.com/'; ?>
    <p><?php _e( ( "To get started with your subsite, please visit %s and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain); ?></p>

    The subsite gets created perfectly, but the checkout process hangs because my syntax is wrong here. Can you help? TIA.

  • Vinod Dalvi
    • WP Unicorn

    Hi Burlington,

    Could you please try using the following code in place of your shard code?

    <?php $user_info = get_userdata(get_current_user_id()); ?>
    <?php $cmdomain = 'https://' . $user_info->user_login . '.example.com/'; ?>
    <p><?php printf( __( "To get started with your subsite, please visit %s and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain); ?></p>

    Regards,

    Vinod Dalvi

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Works perfectly, thanks! How do I make the link clickable? For the 3rd line above, I tried:

    <p><?php printf( __( "To get started with your subsite, please visit <a href=%s>%s</a> and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain); ?></p>

    but it doesn’t work.

  • Ash
    • WordPress Hacker

    Hello @Burlington Avenue

    Please try the following:

    <p><?php printf( __( "To get started with your subsite, please visit <a href='%s'>%s</a> and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain, $cmdomain ); ?></p>

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers

    Ash

  • Burlington Avenue
    • Site Builder, Child of Zeus

    One more (hopefully) quick question: How do I prevent the URL from containing non-alphanumeric characters? That is, if a user selects a username containing -._~:disappointed:?#[]@!$&'()*+,;= , how can we modify the code so that these characters are stripped out of the URL created at user registration? TIA.

  • Ash
    • WordPress Hacker

    Hello @Burlington Avenue

    Would you please try one of the following?

    <?php
    $user_info = get_userdata(get_current_user_id());
    $userDomain = str_replace( array( '-', '.', '_', '~', ':', '/', '?', '#', '[', ']', '@', '!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=', ',' ), array( '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ), $user_info->user_login );
    $cmdomain = 'https://' . $userDomain . '.example.com/';
    ?>
    <p><?php printf( __( "To get started with your subsite, please visit <a href='%s'>%s</a> and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain, $cmdomain ); ?></p>

    or

    <?php
    $user_info = get_userdata(get_current_user_id());
    $userDomain = str_replace( array( '-', '.', '_', '~', ':', '/', '?', '#', '[', ']', '@', '!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=', ',' ), '', $user_info->user_login );
    $cmdomain = 'https://' . $userDomain . '.example.com/';
    ?>
    <p><?php printf( __( "To get started with your subsite, please visit <a href='%s'>%s</a> and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain, $cmdomain ); ?></p>

    Please let me know if it works.

    Cheers

    Ash

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Thanks, Ash. Your first solution works fine for my order-received and email confirmation scripts. Now what about the code Vinod provided initially for auto-creating the blog? That code currently creates a blog at username.example.com without stripping anything out. Thanks!

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Hey no worries, got it straightened out. Just needed to add your str_replace solution to the blog create function.

    In stripping out characters, there is a remote possibility of duplicating URLs. For example, username “users@4example.com” would get the same URL as username “use@rsexample.com”. Any advice on avoiding that? Much appreciated, thanks!

  • Vinod Dalvi
    • WP Unicorn

    Hi @Burlington Avenue,

    To avoid duplicate URL condition you can just check whether the site already exists using the function domain_exists and if it exists then try appending 1 … 2 … 3 … so on to the site URL so the site URL will become like admin1.example.com … admin2.example.com … admin3.example.com and create the site if the domain doesn’t exist.

    So your previously provided code will become something like following.

    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 = '.example.com'; // Add your main domain here
    $userDomain = str_replace( array( '-', '.', '_', '~', ':', '/', '?', '#', '[', ']', '@', '!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=', ',' ), '', $user_info->user_login );
    for ($i=0; $i < 1000; $i++) {
    $new_domain = ($i) ? $userDomain.$i.$main_site : $userDomain.$main_site;
    if( ! domain_exists( $new_domain, '/' ) ){
    wpmu_create_blog( $new_domain, '/', $user_info->user_login, $user_id );
    break;
    }
    }
    }

    }

    Best Regards,

    Vinod Dalvi

  • Burlington Avenue
    • Site Builder, Child of Zeus

    This creates the correct subsite beautifully, Vinod. No duplicates; perfect, nice trick, very elegant. The problem now is with the thank-you page and email confirmation. When I substitute your new code there, it adds another ‘1’ to the subdomain just created in order to avoid duplicating it. How do I update Ash’s code above to capture the subsite created by your solution? TIA.

  • Vinod Dalvi
    • WP Unicorn

    Hi @Burlington Avenue,

    To make it work with the thank-you page and email confirmation, you can try using the following code replacing previously provided code.

    <?php
    $user_blogs = get_blogs_of_user( get_current_user_id() );
    if( !empty($user_blogs) ){
    foreach ($user_blogs AS $user_blog) {
    if( !is_main_site( $user_blog->userblog_id ) ){
    $userDomain = $user_blog->blogname;
    }
    }
    }
    $cmdomain = 'https://' . $userDomain . '.example.com/';
    ?>
    <p><?php printf( __( "To get started with your subsite, please visit <a href='%s'>%s</a> and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain, $cmdomain ); ?></p>

    Best Regards,

    Vinod Dalvi

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Thanks, Vinod. I used ‘domain’ instead of ‘blogname’, and removed .example.com/.

    <?php
    $user_blogs = get_blogs_of_user( get_current_user_id() );
    if( !empty($user_blogs) ){
    foreach ($user_blogs AS $user_blog) {
    if( !is_main_site( $user_blog->userblog_id ) ){
    $userDomain = $user_blog->domain;
    }
    }
    }
    $cmdomain = 'https://' . $userDomain;
    ?>
    <p><?php printf( __( "To get started with your subsite, please visit <a href='%s'>%s</a> and complete the brief setup. Your order details are shown below for your reference:", 'woocommerce' ), $cmdomain, $cmdomain ); ?></p>

    Otherwise, that was perfect. Much appreciated!

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Hello, there. For some reason, this solution is no longer working as well as it did. Subsites are getting created automatically, but the order confirmation is not being sent. I suspect a WC upgrade broke something. I have not edited the code provided in this solution. Anyone care to take a look?

  • Kasia Swiderska
    • Support nomad

    Hello Burlington Avenue,

    Did you check if without that code order confirmation are sending? If not, that is not issue with that particular code and you should create new thread for separate issue. That is long thread and adding another problem that its not about original question will delay in resolving it.

    kind regards,

    Kasia

  • Burlington Avenue
    • Site Builder, Child of Zeus

    I believe the problem is at least partially with the code created here. There are 3 pieces of code: (1) a script (plugin Auto Create Blog) that creates a subdomain subsite at user registration in WooCommerce, (2) a script for a custom WooCommerce thank-you page that lists the subdomain URL and other dynamic text, and (3) a script for a custom WooCommerce email confirmation that lists the subdomain URL and other dynamic text.

    (1) A new blog is auto-created at registration with the username as subdomain; the script creates a second subdomain when purchased, but it does not create a third or additional subdomains.

    (2) The custom thank-you page works properly when activated.

    (3) It’s true that no order confirmation is sent to the customer whether the script is active or not. However, the website admin receives an order confirmation whether the script is active or not, so WooCommerce is operating properly.

    I could really use some help. I think the script may need simple updating. May I grant support access to the site?

  • Kasia Swiderska
    • Support nomad

    Hello Burlington Avenue,

    (3) It’s true that no order confirmation is sent to the customer whether the script is active or not. However, the website admin receives an order confirmation whether the script is active or not, so WooCommerce is operating properly.

    If customer is not receiving order conformation with or without code and admin gets email in both cases then its not problem with code, but with Woo – or this is conflict that appear with latest changes on your blog.

    Have you excluded problem with other plugin by running plugin conflict?

    http://premium.wpmudev.org/manuals/using-wpmu-dev/getting-support/

    Did you try using SMTP plugin? This might sound strange but I’ve seen those plugin help with cases like that – when only part of the emails are send and some are not.

    https://srd.wordpress.org/plugins/wp-mail-smtp/

    https://srd.wordpress.org/plugins/easy-wp-smtp/

    kind regards,

    Kasia

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Order confirmation emails are now being sent correctly, thank you. (Incorrect setting in WC.)

    The only remaining problem is with the script here, particularly issue (1) above. The script successfully creates a blog user.example.com when a new user registers. A second blog user1.example.com is created when the user purchases again. But on third purchase, the third blog has the same address as the second.

    Nothing in the script has been changed. Any suggestions for tracking down the error?

  • Rupok
    • Support Ninja

    Hi Burlington Avenue,

    Thanks for granting Support Access. I logged in and found the coupon code in your Staff Note area, thanks for that.

    Then I logged out and purchased from your site with that coupon code. In thank you page, my subsite URL (wpmutest.yoursite.com) was shown. Then I purchased again with the same coupon code, but weirdly the same subsite URL (wpmutest.yoursite.com) was shown again, a new subsite was not created. Then I logged in with Support Access again to check if multiple subsites were created, but I could find only the first subsite in “Dashboard > My Sites > Network Admin > Sites” section. So I’m not sure if I’m missing any step. Can you please guide me so I can see the issue in action?

    I’m looking forward to hear from you and resolve this issue as soon as possible.

    Have a nice day. Cheers!

    Rupok

  • Burlington Avenue
    • Site Builder, Child of Zeus

    Perfect.

    The plugin Auto Create Blog contains the script created on this thread. It isn’t working as it once did. It’s supposed to create a subsite username.example.com when a user registers and purchases a site. That works.

    But when the same user purchases another site, it’s supposed to create username2.example.com. That doesn’t work.

    The script hasn’t changed. Perhaps a WC upgrade made it necessary to update the script? I’d like help making it work again. Thanks.

  • Rupok
    • Support Ninja

    Hi Burlington Avenue,

    I believe, you are referring to the code shared in this reply, right? https://premium.wpmudev.org/forums/topic/auto-create-subsite-at-user-registration#post-930846

    If yes, can you please check this line?

    add_action( 'user_register', 'user_registration_create_site', 10, 1 );

    This line says that when “user_register” action is called, it should also call another function “user_registration_create_site” which was defined just after this line. That’s why, when a new user *registers* on your site, a subsite for him is created as soon as he registers. But in that code, there is no reference to a subsite creating function hooked to order complete action. That’s why when a logged in user purchases again, a new subsite is not being created for him/her.

    As you said “A second blog user1.example.com is created when the user purchases again”, I’m not sure how that’s happening. With given code, it’s only possible if your user signs up for another account on your site. As subsite creation doesn’t depend on purchasing, so it doesn’t matter how many times he/she purchases a product. A new registration > a new site, that’s the policy of the code above.

    However, if you want to hook your subsite creation function to a successful purchase action instead of user registration action, you can replace the following line:

    add_action( 'user_register', 'user_registration_create_site', 10, 1 );

    with this line:

    add_action( 'woocommerce_order_status_completed', 'user_registration_create_site', 10, 1 );

    I’ve not tested this, but theoretically it should work. Please note, if you do this change, when a new user signs up on your site, a subsite won’t be created automatically for him/her, but every time they create a successful order, a subsite will be created for them.

    To get more idea about related hooks in WooCommerce, you can check this article: http://squelchdesign.com/web-design-newbury/woocommerce-detecting-order-complete-on-order-completion/

    Please let us know how it goes and if you have any confusion. We will be glad to help.

    Have a nice day. Cheers!

    Rupok

  • Burlington Avenue
    • Site Builder, Child of Zeus

    This solution does not work, unfortunately. woocommerce_order_status_completed does not trigger, so although the order is completed and order confirmation email is sent, no subsite is created and the thank-you page does not link to one.

    What else can I try?

  • Rupok
    • Support Ninja

    Hi Burlington Avenue,

    So sorry to see that it didn’t work for you. However, I’m pinging one of our SLS (Second Level Support) guys to have a look at this. If this is something they can fix within 10-15 minutes, they will surely help.

    Please keep in mind, our SLS Guys work around the clock and they have to deal with lots of critical issues and other things. So it may take a little while for them to check this and provide a feedback.

    Have a nice day. Cheers!

    Rupok

  • Burlington Avenue
    • Site Builder, Child of Zeus

    `add_action( ‘user_register’, ‘user_registration_create_site’, 10, 1 );

    /* suggested but didn’t work

    add_action( ‘woocommerce_order_status_completed’, ‘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 = ‘.example.com’; // Add your main domain here

    $userDomain = str_replace( array( ‘-‘, ‘.’, ‘_’, ‘~’, ‘:’, ‘/’, ‘?’, ‘#’, ”, ‘@’, ‘!’, ‘$’, ‘&’, “‘”, ‘(‘, ‘:wink:’, ‘*’, ‘+’, ‘,’, ‘;’, ‘=’, ‘,’, ‘ ‘ ), array( ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ” ), $user_info->user_login );

    for ($i=0; $i < 1000; $i++) {

    $new_domain = ($i) ? $userDomain.$i.$main_site : $userDomain.$main_site;

    if( ! domain_exists( $new_domain, ‘/’ ) ){

    wpmu_create_blog( $new_domain, ‘/’, $user_info->user_login, $user_id );

    break;

    }

    }

    }

    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.