4

I am setting up a Wordpress multisite network, where users may subscribe to one or more services on the main site and may have access to content on subsite. The access on subsite will depend on the service they subscribe on the main site.

What I want to do using some code is when a user registers on main site, I can add that user to the subsite and add some attribute/meta-data to define what access the user has on the subsite (may be I will use memberpress to manage the access)

My main question is what code can be used to add a user to subsite when he signs up main site?

  • Do not have time to develop custom code to achieve this but if you are comfortable to use plugin then this plugin will help you to achieve this. wordpress.org/plugins/join-my-multisite – Vinod Dalvi Jun 29 '17 at 11:50
  • Thanks @VinodDalvi though I need the help in code preferably as I would need to do more things attached to this i.e. the user will be signed up through a Woocommerce purchase on main site and then user will have selective access by enabling a specific membership type based on what they purchase on main site. – nitigyan Jun 29 '17 at 11:56
6

Run add_user_to_blog() after user creation.

You can hook on user_register() to get newly created user ID and pass it and any conditional assignments to your callback that runs add_user_to_blog().

  • Does this mean @skotperez's answer is more correct, or does this work also? He references wpmu_activate_user... just curious. – Drewdavid Aug 29 '18 at 15:56
2

I complete @hwl's answer:

Most of the times, in a Multisite instance of WordPress, user registration includes an activation step.

In this case, it is necessary to hook actions on wpmu_activate_user; actions hook on user_register will be ignored. For instance:

my_function($user_id)   {
    // to add a user to main site (id=1) as a subscriber add_user_to_blog can be used
    // more info: https://codex.wordpress.org/Function_Reference/add_user_to_blog
    add_user_to_blog( '1', $user_id, 'subscriber' );
}
add_action('wpmu_activate_user','my_function',10,1);
  • Hi @skotperez... I was wondering, could you possibly give an example of how the question could be solved entirely? Ie to take a multiuser registration event and cause it to create a new user on the main site (ie, "// do stuff"...)? – Drewdavid Aug 29 '18 at 15:55
  • To add a user to any site of the multisite you can use the add_user_to_blog function. So you can include this function instead of the "do stuff" comment. I edit my answer to include this code. – skotperez Sep 3 '18 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.