wordpresszapierlearndash

Why is the new lesson not linked with the provided course?


I'm managing a LearnDash LMS on a WordPress site. For each new lesson, one of the teachers would publish a video (for exemple, on Youtube or Vimeo). I would then manually take the embed link for this video, create a dedicated lesson in an existing course, and put it in the content for students to watch.

Lately, I've been working on automating this process through Zapier. I had to use LearnDash API v2, and everything was working as expected except that the created lesson won't be "linked" to a course, even though I clearly specify the course id in the request.

Here's what the call looks like:

The POST request is made to this endpoint <website>/wp-json/ldlms/v2/sfwd-lessons

The headers are as follows (authentication headers are indeed excluded):

Content-Type: application/json
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

The body:

{
  "title": "Course title",
  "content": "Embed link",
  "course": Course-ID,
  "status": "publish", 
  "menu_order": 1
}

Each of these attributes is taken into account except "course".

Many thanks to the kind souls who will pass by to help me with this issue.


Solution

  • After days of reading LearnDash documentation, I found a way to add a specific lesson as a course step, though not directly through the standard LearnDash endpoint. You should create your own endpoint (through the current theme or a dedicate Wordpress plugin), and start coding with the following logic:

    $lesson_data = array(
        'post_title'    =>  $lesson_title,
        'post_content'  =>  $lesson_content,
        'post_type'     =>  'sfwd-lessons', // In case of a lesson
        'post_status'   =>  $status,        // 'publish', 'draft', ...
        'post_author'   =>  $author_id      // Optional: by default the current user id
        'menu_order'    =>  $order          // Optional: An Integer that will manage in which order steps are displayed, by default 0
    );
    

    And there you are! Please note that the post_type attribute should be a custom type from LearnDash (lesson, quiz, chapter...). Also note that if you write your code this way, it will append the newly step to the very end of your course steps, that's the by default behavior. If you want to customise the order, use menu_order attribute when creating your new step.

    At this point it looks like a workaround as it doesn't go through the standard LearnDash endpoint, but I'd say so far it's a pretty reliable way to do it, since you're not messing with the database itself but rather using standard WordPress and LearnDash functions.

    Hope this helps, don't hesitate to ask if you have any question on this specific issue.