[CoursePress Pro] how to associate a student to a membership automatically

I want associate my user with a memrship 'student' on membership 2 with coursepress pro student. How to do this automaticly ?

  • James Morris

    Hello Michel,

    I hope you are well today.

    It might be a good idea to explain how Membership 2 and CoursePress handles users to give you a little better idea of how these two plugins work together.

    First, Membership 2:
    Membership 2 does not control user registration on your site. Users are still registered through the native WordPress functions. Membership 2 just defines their access level based on subscribed membership.

    CoursePress:
    Likewise, CoursePress also does not have it's own users. These users are also just WordPress users. CoursePress only controls access to the courses defined in it.

    The way that M2P/CP integration works, M2P recognizes the custom content types in CP and will control access to that content based on membership subscription. To add users to M2P, they must either subscribe through the front-end of your site using one of the M2P subscription options. Or, if you need to add existing users to a membership, you can do so using the native Membership 2 Import tool. This tool allows you to upload a CSV file with the users you would like to associate with a membership.

    To get your CSV of users, you will need to use a plugin like Export Users to CSV. You will need to export all users and do some minor cleanup of the resulting CSV to match up the format of the M2P sample CSV.

    In the user export CSV, you will need to look for and sort by the column "role" this will tell you which users are students. You can then delete all other user rows from the CSV except students and then delete all other columns except username, email, firstname and lastname. Finally, you will want to add a membershipid column to your CSV that contains the ID of the membership you wish to add the users to.

    I've attached some sample CSVs to illustrate how you will need to manipulate the data to get this to work.

    I hope this clarifies a bit. Let us know if you have any further questions. We'll be happy to help! :slight_smile:

    Best regards,

    James Morris

  • Ash

    Hello Michel

    This is possible but there is no direct integration. You have to make your hand dirty with code. You can use the following code:

    add_action( 'ms_model_relationship_create_ms_relationship_before', 'ms_controller_member_assign_memberships_done_cb', 99, 4 );
    function ms_controller_member_assign_memberships_done_cb( $membership_id, $user_id, $gateway_id, $move_from_id ) {
    	$course = array(
            // 'membership_id' => 'course_id'
            '1324' => 6656,
            '9087' => 22345
        );
    
        CoursePress_Data_Course::enroll_student( $user_id, $course[$membership_id] );
    }

    Make sure you configure $course array as suggested.

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.