Can I associate a Membership with a Pro-sites level?

We would like to give all users that sign up to a certain Pro Sites level, access to a specified membership level (restricted access to premium content on the main site)

How can this be achieved?

Kind Regards
Johanna

  • Ivan Shulev

    Hey Johanna ,

    I am sorry for the late reply!

    You can try adding the following in either a Must-use plugin (https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/) or any code snippet plugin:

    function wpmu_5433_assign_membership() {
    	global $psts;
    	$blog_id = get_current_blog_id();
    	$pro_sites_level = 2;
    	if($pro_sites_level == $psts->get_level( $blog_id )) {
    		$uids = get_users(
    			array(
    				'blog_id'  => $blog_id,
    				'role'     => 'administrator',
    				'fields'   => 'ID',
    				'order_by' => 'ID',
    				//No need for super admin
    				'exclude'  => array(1),
    				'order'    => 'ASC',
    				)
    			);
    		//Make sure there are users and MS has initiated
    		if ( apply_filters( 'ms_active', false ) && isset($uids[0])) {
    			$api = ms_api();
    			$subscription = null;
    			//Membership IDs
    			$memberships = array(1417);
    			$current_member = $api->get_member($uids[0]);
    			$assigned_ids = $current_member->get_membership_ids();
    			switch_to_blog(1);
    			if(is_array($assigned_ids) && !empty($assigned_ids)) {
    				foreach ( $current_member->get_membership_ids() as $old_id ) {
    					if ( !in_array( $old_id, $memberships ) ) {
    						$current_member->drop_membership( $old_id );
    						foreach ($memberships as $new_id) {
    							$subscription = $current_member->add_membership($new_id);
    						}
    					}
    				}
    			}
    			else {
    				foreach ($memberships as $new_id) {
    					$subscription = $current_member->add_membership($new_id);
    				}
    			}
    			restore_current_blog();
    		}
    	}
    }
    //This action is fired if the current site has an active level
    add_action('psts_active', 'wpmu_5433_assign_membership');

    What you need to change is the $pro_sites_level = 2; to the level you want and the $memberships = array(1417); to the membership IDs.

    You can get the membership ID by going to WP Admin > Membership 2 > Memberships, like so:

    You can also include multiple memberships like $memberships = array(1417, 1420);

    I hope this helps and I wish you an awesome day ahead!

    Ivan