Assign a membership to a registered member automatically – Membership 2 (WPMU)

WPMU DEV has released the new revamped version for membership plugin, called Membership 2. The layout and interface is awesome of the plugin and I am personally impressed. There is a new type of membership introduced in the plugin – Default Membership. This membership will work for the user who are just registered but didn’t choose any membership to join.

But what if you want to assign a regular membership to that user? Here is a simple code snippet that will help you to do this job:


You may also read:  How to apply coupon automatically on WPMU membership2 checkout
<?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();
}

view raw

code.php

hosted with ❤ by GitHub

Just make sure you change to correct membership ID for $membership_id variable.

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.

You may also like...

2 Responses

  1. Thomas says:

    This is exactly what I needed, thanks!

Leave a Reply

%d bloggers like this: