Enrol Users Into A LearnDash Course Or Group Using A Storyline Trigger

How To Enrol Users Into A LearnDash Course (ID)

Version 2.12 of the eLearning Magic Toolkit introduced a number of new automated actions built for WordPress platforms using the LearnDash LMS plugin for delivering online courses.

Using the first code snippet provided directly below, you can create an action within your Storyline eLearning content that will result in the user being enroled into the selected course on your platform.

Just make sure that the course ID number, included in the very last line of the code snippet below, matches with the course that is set up on your platform. This will work for all published courses including closed, free, and buy now type LearnDash course types:

async function enrolUser(action, id) {
    const myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    myHeaders.append("X-WP-Nonce", parent.storylineMagicRestNonce);

    const response = await fetch(parent.storylineMagicEndpointLearndashActions, {
        method: 'POST',
        headers: myHeaders,
        body: JSON.stringify({ action, id })
    });

    if (response.ok) {
        console.log('Enrolment successful');
    } else {
        console.error('Enrolment failed');
    }
}

// Example usage
enrolUser('Enrol_To_Course', 1); // Replace number with the LearnDash course ID

How To Enrol Users Into A LearnDash Group (ID)

Choose this action instead if you would prefer to add users to a LearnDash group, which can potentially enrol users into multiple courses at the same time on your WordPress/LearnDash platform:

async function enrolUser(action, id) {
    const myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    myHeaders.append("X-WP-Nonce", parent.storylineMagicRestNonce);

    const response = await fetch(parent.storylineMagicEndpointLearndashActions, {
        method: 'POST',
        headers: myHeaders,
        body: JSON.stringify({ action, id })
    });

    if (response.ok) {
        console.log('Enrolment successful');
    } else {
        console.error('Enrolment failed');
    }
}

// Example usage
enrolUser('Enrol_To_Group', 1); // Replace number with the group ID

Read the rest of our Knowledge Base pages to make the most of eLearning Magic Toolkit.

0
0