skip to Main Content

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".

  • I also tried the exact same request through Postman, with the exact same results.
  • Everytime, in the response body, it is shown "course": 0.
  • I’ve tried with several Course-IDs, hard-coded or not, but I still can’t figure out why it doesn’t work or if I’ve missed an important step to make the lesson linkable somehow.
  • The only way to date to link a lesson I create through this API is to do it manually through the LMS Dashboard, which is a pity compared to the original aim.

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

2

Answers


  1. Chosen as BEST ANSWER

    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:

    • You fetch for the desired course_id
    • You create a new parentless child not attached to any course using the wp_insert_post($lesson_data) function. The array passed in args should look like this:
    $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
    );
    
    • Then, you add this newly created lesson to an existing course with this function: learndash_course_add_child_to_parent($course_id, $lesson_id, $course_id)

    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.


  2. Did you manage to get your approach to work for topics as well? I am struggling to associate topics (that are already part of a course) with other courses via the Learndash API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search