Enroll Specific Membership in a Single Coursepress Course

I can register new users to a particular course (in this case the ID is 122) by using the following:

add_action('user_register', 'enroll_for_bgrating');
function enroll_for_bgrating($user_id)
{
    $student = new Student($user_id);
    $student->enroll_in_course(122);
}

The problem is that this enrolls ALL new users but this course is only accessible to a particular membership type.

How can I auto enroll all members of a particular membership into a specific course?

  • Tyler Postle

    Hey Peter,

    This is above my level of coding; however, I'll call in one of our second level support(coding experts) to have a look here.

    I'm thinking this should be relatively quick solution, but I am required to forewarn that if it ends up being more complex than it looks and would take awhile to code up then our staff won't be able to offer support for it ourselves and best option would be to go to our Jobs Board to hire a developer from the community: https://premium.wpmudev.org/wordpress-development/

    Again, I don't think this one will be a problem, but just need to make that clear in any custom development situation :slight_smile:

    Since our SLS staff deal with more complex issues it can take them a little longer to reply as well, once they have had a look then they will reply here.

    Hope you have a great weekend and if there are any other questions in the meantime then feel free to ask or create a new ticket if it's not related to this one: https://premium.wpmudev.org/forums/#question

    Cheers,
    Tyler

  • Ash

    Hello Peter

    The following code will add an user to a course when the user registers for a certain membership:

    add_action(
        'ms_model_relationship_create_ms_relationship_before',
        function( $membership_id, $user_id, $gateway_id, $move_from_id ) {
            /**
             * 'membership_id' => 'course_id',
             */
            $array = array(
                '1023' => '234',
                '2341' => '265',
                '2763' => '165'
            );
    
            $student = new Student( $user_id );
            $student->enroll_in_course( $array[$membership_id] );
        },
        99, 4
    );

    You need to configure the array based on membership IDs and course IDs.

    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

  • Ash

    Oh and to add all members of certain membership to a specific course you can try this code:

    function enrol_members_to_course( $membership_id, $course_id )
    {
        $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
        $members = $membership->get_members();
    
        foreach( $members as $user_id => $user )
        {
            $student = new Student( $user_id );
            $student->enroll_in_course( $course_id );
        }
    }
    
    enrol_members_to_course( 123, 456 );

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

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.