User Role and Membership

I have a mobile APP that gets the user role via the WP REST API.

I am setting up a multisite with membership2 plugin.

I want when a user registers one of the 4 memberships, he gets a specific user role.

I understood membership 2 can do that, so, I activated the Member Capabilities add on.

In Membership 2 -> Protection Rules -> user roles I set up that eg. somebody who registers for the membership ‘FREE’ the role ‘Hypnotist_free’ should be assigned to this user.

And then tested this: registered a user. In his account page, I can see the Membership Name is ‘FREE’.

When I log in as administrator and go to the list of all users, I see his Role is ‘Subscriber’ instead of ‘Hypnotist_free’.

Is possible to change the User Role depending on the Membership, not only the capability?

Thanks in Advance

  • Konstantinos Xenos

    Hi Paul ,

    You can add this code snippet into a mu-plugin but you'll have to adjust it a bit depending on how many membership and roles you want to add. This will work during the registration and if the members want to change between memberships as well to adjust their role respectively.

    How to install:
    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Inside the mu-plugins folder create a file named m2p-user-roles.php
    3] Edit the file and copy / paste this code snippet inside.

    add_filter(
    	'ms_model_relationship_create_ms_relationship',
    	function( $subscription, $membership_id, $user_id, $gateway_id, $move_from_id ) {
    
    		$user = new WP_USER( $user_id );
    		$role = (array) $user->roles;
    
    		if ( in_array( 'administrator', $role, true ) ) {
    			return $subscription;
    		}
    
    		// change the if / elseif here depending on your roles and membership ids.
    		if ( 30 === $membership_id ) {
    			$user->set_role( 'subscriber' );
    		} elseif ( 43 === $membership_id ) {
    			$user->set_role( 'editor' );
    		}
    
    		return $subscription;
    
    	},
    	15,
    	5
    );

    4] Save and close the file.
    4] The final path should look like /wp-content/mu-plugins/m2p-user-roles.php
    5] Everything should work after that automatically.

    Tell me if you need further help with this!

    Regards,
    Konstantinos

    • Konstantinos Xenos

      Paul ,

      Sorry for this I forgot to add the "<?php" on my snippet for the start of the file, that's why you where seeing the code on the top of your page. Without the "<?php" opening tag the code won't actually execute and will just print out in the page.

      The full snippet would be:

      <?php
      
      add_filter(
      	'ms_model_relationship_create_ms_relationship',
      	function( $subscription, $membership_id, $user_id, $gateway_id, $move_from_id ) {
      
      		$user = new WP_USER( $user_id );
      		$role = (array) $user->roles;
      
      		if ( in_array( 'administrator', $role, true ) ) {
      			return $subscription;
      		}
      
      		// change the if / elseif here depending on your roles and membership ids.
      		if ( 30 === $membership_id ) {
      			$user->set_role( 'subscriber' );
      		} elseif ( 43 === $membership_id ) {
      			$user->set_role( 'editor' );
      		}
      
      		return $subscription;
      
      	},
      	15,
      	5
      );

      Tell me if you need further help.

      Regards,
      Konstantinos

  • Paul

    I was too quick to say it works. I had to change hosting, which I did, I am on stronger hosting now. There I made a test again and created 2 test users again. First one, username: th8 on en.hypnotool.com, She registered via your plugin for a "FREE" account. Now she has the role but NO membership... I did a second test registering user: th9 on nl.hypnotool.com, who registered for a 'GRATIS' account (gratis is dutch word for FREE). Same result: the user has a role now , but NO membership...

    So... still not working as it should, can you help me? Support is granted access to the site. Thanks a lot, Paul.

  • Paul

    I found the issue, it is a plugin (Force Login) who seems to be too strict for your membership plugin to deal with. The site will be the back-end for a mobile app for therapists. You can understand I do not want Worpress to hick up one or another 'blog' thing to visitors, like searching for an empty string and browsing categories (which is by the way still possible even when I mark them as 'protected' in your plugin, you can still read in categories via eg. search or, if one knows the name of the category...)

    So, I installed plugin 'Force Login' and whitlisted the pages and bypassed $_SERVER['QUERY_STRING'] to allow eg. password reset etc.

    When this plugin is activated, your plugin allows user creation, but without making the user a member. When I de activate the Force Login plugin, it all works as it should be.

    It looks like I will have to whitelist some more possibilities in the Force Login plugin, do you know what you need to be whitlisted or bypassed in : https://nl.wordpress.org/plugins/wp-force-login/

    Thanks 4 help ! Paul.

  • Konstantinos Xenos

    Hey Paul ,

    I've added the Force Login to take a look myself and the only thing that I allowed was the registration page ( that Membership adds by default ). I white-listed it for the plugin by doing this according to their documentation and everything worked fine on my end. There was no need to bypass any other page at least for the registration part to work properly and the memberships where added correctly to my users.

    // Bypass the default /register Membership page.
    add_filter(
    	'v_forcelogin_bypass',
    	function ( $bypass ) {
    		if ( is_page( 'register' ) ) {
    			$bypass = true;
    		}
    		return $bypass;
    	}
    );

    Unfortunately since our set up isn't the same obviously there might be more that I'm missing, but it could be hard to replicate as well.

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.