Automatically enroll members to a course

Please advise, I’m looking for a way to integrate Membership 2 Pro with CoursPress. All members should have access to courses. Is there is a way to automatically enroll all users into a course?

The shortcode [course_join_button course_id="1901"] is redirecting to the CourseOverview page. Instead if this, can this shortcode enroll users automatically without a redirect?

  • Adam Czajczyk
    • Support Gorilla

    Hello Etienne

    I hope you’re well today and thank you for your question!

    This is not possible out of the box currently. However, we have asked our developers to take a look into it and see if we could provide you with some workaround/code snippet for this so please keep an eye on this ticket and we’ll update you here as soon as possible.

    Best regards,

    Adam

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi Etienne ,

    You can automatically add students when they choose a membership by adding this snippet:

    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 auto-add-student.php

    3] Edit the file and copy / paste this code snippet inside.

    <?php

    add_filter(
    'ms_model_relationship_create_ms_relationship',
    function( $subscription, $membership_id, $user_id, $gateway_id, $move_from_id ) {
    // Change the "1937" with the Membership ID you want to check for.
    if ( 1937 === $membership_id ) {
    // Change the "2073 " with the Course ID you want to add the students.
    CoursePress_Data_Course::enroll_student( $user_id, 2073 );
    }

    return $subscription;
    },
    15,
    5
    );

    4] Save and close the file.

    5] The final path should look like /wp-content/mu-plugins/auto-add-student.php

    6] Everything should work after that automatically.

    I’ve added your Membership (1937) + Course ( 2073 ) IDs in the code so it should start working as soon as you create the plugin but do note that the snippet will have to be updated if you want more courses or memberships involved.

    Tell me if you need further help!

    Regards,

    Konstantinos

  • Etienne
    • Design Lord, Child of Thor

    Awesome, thank you for the fast reply.

    Is there a way to auto add students to any courses? Not restricted to only one course ID? I’m asking because we will offer a new course every month. All members of the (1937) membership will have access to the monthly courses. Otherwise, I would have to change the course ID in the snippet every time I add the course of the month. Which is not super time-consuming, but if it could be automated it would be awesome!

    Maybe I could create all my future course in advance so that they already have their ID generated, and we can put multiple IDs in the snippet?

    Let me know if there is a solution to this. Thanks!!!

  • Adam Czajczyk
    • Support Gorilla

    Hi Etienne

    I think the simplest solution here would be to, as you say, create all the courses in advance and then add users “in bulk” to all of them.

    A “future course” doesn’t even have to be complete or “visible” – you can e.g. keep it as “draft” until time comes as long as it’s there. Then you can make a small change to the code that Konstantinos shared by simply multiplying this line:

    CoursePress_Data_Course::enroll_student( $user_id, 2073 );

    For example, let’s say there are three courses of IDs 123, 456, 789 accordingly. You’d just replace that single line with 3 “copies” of it like this:

    CoursePress_Data_Course::enroll_student( $user_id, 123 );
    CoursePress_Data_Course::enroll_student( $user_id, 456 );
    CoursePress_Data_Course::enroll_student( $user_id, 789 );

    Note: if a given member was already auto-added to a course using initial version of the snippet, you’ll still have to manually add that member to other courses. But for all new members (that will choose this membership after the code is updated) it should work just fine and add them to all specified courses :slight_smile:

    Best regards,

    Adam

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Etienne ,

    One way would be as Adam mentioned and updating the code with the courses ‘manually’ would be the safest just in case you don’t want them to accidentally enroll on a course that you have not yet fully prepared.

    If you want that to happen a bit more ‘automated’ you can update my previous code with this :

    This will fetch all Courses that are with a “publish” state and will enroll the student into all of them. As I’ve mentioned above though since this is automated if you accidentaly publish a course and a member happens to signup at that time they will be enrolled as well. If that isn’t an issue then you can surely used this code :slight_smile: .

    <?php

    add_filter(
    'ms_model_relationship_create_ms_relationship',
    function( $subscription, $membership_id, $user_id, $gateway_id, $move_from_id ) {

    $args = array(
    'post_type' => 'course',
    'posts_per_page' => -1,
    'post_status' => array( 'publish' ),
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() && 1937 === $membership_id ) {
    while ( $query->have_posts() ) {
    $query->the_post();
    CoursePress_Data_Course::enroll_student( $user_id, get_the_ID() );
    }
    wp_reset_postdata();
    }

    return $subscription;
    },
    15,
    5
    );

    I hope this helps!

    Regards,

    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.