admin管理员组

文章数量:1322180

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?

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?

Share Improve this question asked Jun 29, 2017 at 10:02 nitigyannitigyan 1635 bronze badges 2
  • 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/plugins/join-my-multisite – Vinod Dalvi Commented Jun 29, 2017 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 Commented Jun 29, 2017 at 11:56
Add a comment  | 

2 Answers 2

Reset to default 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().

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/Function_Reference/add_user_to_blog
    add_user_to_blog( '1', $user_id, 'subscriber' );
}
add_action('wpmu_activate_user','my_function',10,1);

本文标签: Wordpress MultisiteWhen a user signsup on main sitehow to add the user to a subsite