Using Gravity Forms with Membership to handle registration

So, in the documentation for Membership, you have this information that I would like to get more details on:

If you’re using a plugin to create a custom registration page, like BuddyPress, Pie Register, or Gravity Forms, be sure to select THAT registration page here, rather than the default one created by the Membership2 plugin.

If you are using another plugin for registration, be sure to add the following code to your theme’s functions.php file so the Membership2 plugin knows about it (this is not required for BuddyPress, but it won’t interfere if you add it anyway):
add_filter('ms_frontend_handle_registration', '__return_false');

That is interesting, but not very descriptive about how that works. I have a client who needs a good membership plugin that works extremely well with BP. I know this one does, but they need to use Gravity Forms for registration because there is a lot of data that needs to be captured that extended profiles is not appropriate for as the memberships are corporate memberships, not personal ones. Thing is, Gravity Forms Registration creates new user accounts based by assigning them a user role. The big question I have is, how does your memberships plugin handle user permissions? Does it create unique user roles for each level? If so, I can see how I could use GF Registration to sign new accounts up and have them assigned the correct level in Membership.

Ideally what we I'm thinking about is using Gravity Forms to handle the registration and let the Membership plugin handle the access control, but given how GF handles registration by assigning the new account a user role, how does that play into the methods that Membership uses?

  • Nithin

    Hi Saunt Valerian,

    Hope you are doing good today. :slight_smile:

    add_filter('ms_frontend_handle_registration', '__return_false');

    What this filter does is stop Membership 2 Pro from handling the registration process, so that you can make use of your Gravity forms register page. Once you have set the Gravity forms register page, under Membership 2 > Settings > General > Registration. You can add the above line in your themes functions.php file to unset the URL to the defualt Membership registration page.

    The big question I have is, how does your memberships plugin handle user permissions? Does it create unique user roles for each level? If so, I can see how I could use GF Registration to sign new accounts up and have them assigned the correct level in Membership.

    By default users get a subscriber roles for all membership levels, so if you are handling this through Gravity forms the roles assigned by the Gravity forms should be the default ones. So, implementing the user roles through Gravity forms should handle the registration part, and it should work fine.

    You could make use of the Member Capabilities addonlocated under, Membership 2 > Add-ons to change roles according to membership levels. What the addon does is, ability to assign user roles automatically to members who signups to a specific membership levels. Once enabled, you can assign user roles under Membership 2 > Protection Rules > User Roles.

    I hope this helps. Please advise if I had missed out anything. Have a nice weekend. :slight_smile:

    Kind Regards,
    Nithin

  • Saunt Valerian

    So if Membership doesn't create roles that can be assigned on registration via GF, then I'm confused by this:

    You could make use of the Member Capabilities addon located under, Membership 2 > Add-ons to change roles according to membership levels.

    I did some testing of this general idea and it is interesting. However, it seems that the Member Capabilities does not change the user roles according to the membership level, it simply ties a user role to a membership so that when a membership level is active on a user, you can leverage additional functionality associated with that user role.

    When assigning a Membership to any role, then this role will be added to all members of that Membership.

    That's from the settings page for the User Roles tab in the plugin.

    It doesn't explain how, when using alternative registration, Membership knows to assign a membership level to that user.

    I setup a test install to try this all out with a basic GF registration form and I am able to get a user registered into my custom user role (level 1 membership), and I have that user role tied to a membership level (also called level 1 membership), however, the new user account doesn't get assigned a membership level. The user account appears in my standard user list with the correct user role in the admin but not in the All Members list in Membership. In order to complete the registration, I had to go in and manually add the new member to membership using membership's Add Member - Select an existing WordPress user function.

    The question then remains, how does Membership automatically know to assign a membership level to a new account when using an alternative registration method?

    Essentially, this comes down to how to automatically add new members to MS2 at the appropriate membership level based on the user role of the user. This is similar functionality to automation that is part of the BP add-on when the BP registration form is used.

  • Kasia Swiderska

    hello ,

    I did some testing of this general idea and it is interesting. However, it seems that the Member Capabilities does not change the user roles according to the membership level, it simply ties a user role to a membership so that when a membership level is active on a user, you can leverage additional functionality associated with that user role.

    Member Capabilities does not change user role. It adds capabilities for role for user that joined membership. So lets subscribers will get capabilities from editor when they will join membership that gives them that. But role will not be changed.
    So your understanding is right.

    It doesn't explain how, when using alternative registration, Membership knows to assign a membership level to that user.

    Membership does not assign membership on registration. It assign it when user joins membership.
    When you use standard registration form before you choose membership form list, then you go to checkout to join that membership. You can also use standalone registration form and user will be only registered - and then it will have choice of what membership to join. Membership add membership to users only when they join them, not on registration (except the default type of membership, that is added automatically to every user that did not join any membership but its registered).

    When you use alternative method you need
    a) redirect users after registration to memberships lists to join one of them
    b) write custom solution that will assign membership when your user are registering through the Gravity Form

    that would look like that

    <?php
    add_action( 'user_register', 'assign_membership_on_register', 10, 1 );
    function assign_membership_on_register( $user_id ) {
    
          $membership_id = 1343;
          $member = MS_Factory::load( 'MS_Model_Member', $user_id );
          $subscription = $member->add_membership( $membership_id );
    
          if ( $member->has_membership() ) {
              $member->is_member = true;
          } else {
              $member->is_member = false;
          }
          $member->save();
    }

    Essentially, this comes down to how to automatically add new members to MS2 at the appropriate membership level based on the user role of the user.

    To actually change role when member joins membership you would need to use this code snippet https://gist.github.com/bappi-d-great/794bd6a18eecb59f8f27 (this one was wrote by one of our developers)

    Let me know if you have more questions.

    kind regards,
    Kasia

  • Saunt Valerian

    Kasia, I'm finally able to come back and revisit this. That code snippet above is interesting, though I have a couple questions about it.

    $membership_id is what? Is that an identifier number that gets assigned to each membership level? If it is, where do I find it after creating a membership level?

    I want to assign membership levels based on the user role, if I only have two membership levels and two corresponding user roles I need some way to ensure that the two match up and anytime a new user is created in a particular user role, they automatically get the corresponding membership level assigned to them. So my original issue still remains, how to automatically get a membership level (when there are several present, along with several user roles) assigned to a new user based on their user role.

  • Ash

    Hello Saunt Valerian

    An unique ID is assigned when a membership is created. So every membership has an ID, you need to get that ID.

    To find out the ID, go to Membership page in admin, hover over shortcodes and you will see the ID, please check the screenshot.

    To assign membership based on role, let's draw a scenario. For example, role abc will get membership with ID 123 and role xyz will get membership with ID 456. Now you can try the following the code:

    add_action( 'user_register', 'assign_membership_on_register', 10, 1 );
    function assign_membership_on_register( $user_id ) {
    
        $membership = array(
            // 'role' => 'membership ID'
            'abc' => 123,
            'xyz' => 456
        );
    
        $user_meta = get_userdata( $user_id );
        $user_roles = $user_meta->roles;
    
        $member = MS_Factory::load( 'MS_Model_Member', $user_id );
        $subscription = $member->add_membership( $membership[ $user_roles[0] ] );
    
        if ( $member->has_membership() ) {
            $member->is_member = true;
        } else {
            $member->is_member = false;
        }
        $member->save();
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

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

    Cheers
    Ash

  • Saunt Valerian

    I finally had the opportunity to test this out. This is a really solid solution. However, with Gravity Forms, that I needed to trigger the function on a different action other than user_register. This is because GF uses a Pending user role when it initially registers a user and they aren't channeled into the configured user role until the account is activated.

    So, changed the action trigger to set_user_role and it worked great. Line 1 looks like this now:

    add_action( 'set_user_role', 'assign_membership_on_register', 10, 1 );

    Thank you for this, it is a very elegant solution!

  • Saunt Valerian

    So, I need to revisit this again. I've got the code snippet setup in a custom plugin on the site, but it fails to work now. For some reason, it just stopped working. I've done the plugin test and deactivated all the plugins to test this through and it fails to work. I am at a bit a of a loss now.

    I've tried it in both on the original user_register trigger and the set_user_role trigger and nothing happens. The user accounts are not getting passed through into Membership.

    I've granted support access. The site is http://salonspa.staging.wpengine.com/

  • Kasia Swiderska

    Hello Saunt Valerian,

    Can you show me your whole code you are using and tell me what exactly stopped working? Have you debug that code with debug mode in WordPress? If not can you do that? To enable it, open your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

    define('WP_DEBUG', true);

    In order to enable the error logging to a file on the server you need to add yet one more similar line:

    define( 'WP_DEBUG_LOG', true );

    In this case the errors will be saved to a debug.log log file inside the /wp-content/directory.

    Depending on whether you want your errors to be only logged or also displayed on the screen you should also have this line there, immediately after the line mentioned above:

    define( 'WP_DEBUG_DISPLAY', false );

    The wp-config.php is located in your WordPress root directory. It’s the same file where the database configuration settings are. You will have to access it by FTP or SFTP in order to edit it. I would need then to see content of the debug.log file - so please upload it to dropbox like a service and paste link here.

    kind regards,
    Kasia

  • Saunt Valerian

    Kasia, turning on debugging was helpful, I should have thought of that myself.

    When I run through the signup process, the code is throwing an error. Here are the details from the error log relative to this:

    [09-Jan-2017 14:58:35 UTC] PHP Notice:  Undefined offset: 0 in /nas/content/staging/salonspa/wp-content/mu-plugins/user-roles-to-membership.php on line 22
    [09-Jan-2017 14:58:35 UTC] PHP Notice:  Undefined index:  in /nas/content/staging/salonspa/wp-content/mu-plugins/user-roles-to-membership.php on line 22
    [09-Jan-2017 14:58:35 UTC] PHP Notice:  Undefined offset: 0 in /nas/content/staging/salonspa/wp-content/mu-plugins/user-roles-to-membership.php on line 22
    [09-Jan-2017 14:58:35 UTC] PHP Notice:  Undefined index:  in /nas/content/staging/salonspa/wp-content/mu-plugins/user-roles-to-membership.php on line 22

    You can see the entire output of the error log here: http://pastebin.com/eLujyXQu

    I've placed the code for all of this into a mu-plugin, and you can see all of it here: http://pastebin.com/fFZQyfE1

    I currently have it set to trigger the function on set_user_role instead of user_register, this is because Gravity Forms can't do automatic login on registration and the user has to go through a pending role first through account activation.

  • Rupok

    Hi Saunt Valerian,

    So sorry for the inconvenience you are having. I tried to regenerate the issue/error on my test site but I could not. Let me describe how I checked:

    I added the following code in my MU Plugin file:

    add_action( 'set_user_role', 'assign_membership_on_register', 10, 1 );
    function assign_membership_on_register( $user_id ) {
        $membership = array(
            'author' => 11,
            'editor' => 804
            );
        $user_meta = get_userdata( $user_id );
        $user_roles = $user_meta->roles;
        $member = MS_Factory::load( 'MS_Model_Member', $user_id );
        $subscription = $member->add_membership( $membership[ $user_roles[0] ] );
    
        if ( $member->has_membership() ) {
            $member->is_member = true;
        } else {
            $member->is_member = false;
        }
        $member->save();
    }

    So if I change any user role to "Author", that should make that user a member of ID "11". Now, to test this, I changed the role of an existing user on my test site (who was not a member of any membership plan) to "Author" and the moment I saved that user role, he was assigned membership 11. I checked in my error log, but there is not Notice like yours.

    As this function will run when a user has signed up already (though pending, but your user gets a user ID the moment he gets an account), so the array in your code should not return empty value. And such notices should not be occurring. However, I could tell you better about the issue if I could see it live, check your current configuration and make some tests on your site. Can you please send me a message with your FTP and WP access credentials through our secure contact form here: https://premium.wpmudev.org/contact/ so we can investigate the issue further on your site?

    Subject: "Attn: Rupok"
    - FTP Username
    - FTP Password
    - FTP Host
    - WordPress admin Username
    - WordPress admin Password
    - Login URL
    - Link back to this thread for reference
    - Any other relevant URLs

    Select "I have a different question" for your topic. This and the subject line ensure that it gets assigned to me.

    Please note, if this is a live site, then can you please create a clone of your site and provide credentials for that site so we can check extensively? As membership subscriptions are critical, I would not suggest doing this type of risky tests on a live site. Please let us know what you suggest. I'm looking forward to hearing from you and resolving this issue as soon as possible.

    Have a nice day. Cheers!
    Rupok

  • Saunt Valerian

    I've got the process working again. I think the issue was that I had use the WPFront User Role Editor Plugin to delete the built-in user roles in favor of having only custom roles available. Once I restored all the built-in user roles, this method started working again. Even though the Pending user was not deleted (and I guess it doesn't properly exist, in a way), it seems to somehow tie with the others and they needed to be present and available for the process to work.

    I think we can mark this as solved.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.