Auto-enroll in Coursepress courses based on Membership2 membership level

I want to be able to associate specific courses with the different membership levels used in Membership2. I found some code for adding all new site registrations to all courses here:

https://premium.wpmudev.org/forums/topic/coursepress-auto-register-user#post-923142

From that, and some code Ash gave me for a different purpose, I was able to figure out how to do it:

add_action( 'ms_model_relationship_create_ms_relationship_before', 'ms_controller_member_enroll_in_course', 99, 4 );
function ms_controller_member_enroll_in_course( $membership_id, $user_id, $gateway_id, $move_from_id, $course_id ) {
$student = new Student( $user_id );
switch( $membership_id ){
case 280:
$student->enroll_in_course(286);
break;
}
}

So, this does work — but, the username is added into the student registration details as the “First Name” as well as the “Username”.

What would I need to add to my code to pull the user’s first and last names from their user profile and include them as part of the student registration?

Thanks,

Scott

  • scottmcculloch
    • The Incredible Code Injector

    Oops! My mistake…

    It turns out the user account I tested this with had the “First Name” the same as the username… when I changed that, it was changed automatically in the CoursePress student list, so I can see that the student list is just pulling from the current profile info for the user id.

    Consider this resolved! :slight_smile:

    Scott

  • scottmcculloch
    • The Incredible Code Injector

    Hi Kasia,

    It does still work… the error I was getting when I had $course_id in there was:

    Warning: Missing argument 5 for ms_controller_member_enroll_in_course() in /nas/wp/www/staging/trimergence/wp-content/mu-plugins/coursepress-auto-enroll-by-membership.php on line 2

    So, while it now works, it might not be a bad idea to have Ash take a quick look to make sure there isn’t a better or more correct way to do it.

    There is still a problem though (not directly related)… it turns out that when a new user registers his username gets put in the “first name” field (of user profile)… so that also gets put into the students list.

    I’m using Membership2 with BuddyPress — is there a way to set up the registration form (BuddyPress) so that I can capture First Name and Last Name and have them put in the right fields in the main user profile?

    Thanks,

    Scott

  • Kasia Swiderska
    • Support nomad

    Hi Ward,

    I’m using Membership2 with BuddyPress — is there a way to set up the registration form (BuddyPress) so that I can capture First Name and Last Name and have them put in the right fields in the main user profile?

    I’ve just tested this combination – Membership, BuddyPress and CoursePress list students, and this is the default setting.

    If the registered user will fill the Username + First Name + Last name this values are putted to the right places in the User profile, and then they are displayed on the Students list.

    Did you change in anyway the default registration form in BuddyPress?

    Kind regards,

    Kasia

  • scottmcculloch
    • The Incredible Code Injector

    Hi Kasia,

    Upon closer investigation I see that the problem was “operator error” :wink:

    I was testing with a test user account I had set up before I had installed BuddyPress, so that test user didn’t have First Name and Last Name in the profile.

    Testing with a newer test account set up through BuddyPress it all works as expected.

    Thanks,

    Scott

  • scottmcculloch
    • The Incredible Code Injector

    @aknath707

    Just read your post in the other topic I’d responded to before I started this one:

    https://premium.wpmudev.org/forums/topic/coursepress-auto-register-user#post-926422

    Thought I’d let you know I found a solution… and if you’d like to check it to make sure it’s good (i.e., not missing something important, or doing something I’m not seeing that it shouldn’t), that would be cool.

    Thanks,

    Scott

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.