admin管理员组

文章数量:1410737

I have a learnpress course on a wordpress site. The 'complete' button wasn't working (it would 404), so I have removed it and instead want the 'next' button/link to both mark the current lesson as complete and then progress to the next page. I have added custom JS to make an AJAX call when the next button is clicked on a page and that works fine. The problem I have is that server-side (PHP), I'm unable to mark the lesson as complete. I am passing the user id and lesson id to PHP, but then server side I'm not able to locate the course id from the lesson id. I've taken a look at the wp_post_meta table and none of the lessons have a property referring to the course id. Additionally, running the below code in PHP returns 'undefined':

$course_id = get_post_meta($lesson_id, '_lp_course', true);

I am wondering how I can mark a lesson as complete using PHP using the user id and lesson id.

Using learnpress 4.2.8

I have a learnpress course on a wordpress site. The 'complete' button wasn't working (it would 404), so I have removed it and instead want the 'next' button/link to both mark the current lesson as complete and then progress to the next page. I have added custom JS to make an AJAX call when the next button is clicked on a page and that works fine. The problem I have is that server-side (PHP), I'm unable to mark the lesson as complete. I am passing the user id and lesson id to PHP, but then server side I'm not able to locate the course id from the lesson id. I've taken a look at the wp_post_meta table and none of the lessons have a property referring to the course id. Additionally, running the below code in PHP returns 'undefined':

$course_id = get_post_meta($lesson_id, '_lp_course', true);

I am wondering how I can mark a lesson as complete using PHP using the user id and lesson id.

Using learnpress 4.2.8

Share Improve this question asked Mar 7 at 23:19 Often RightOften Right 4252 gold badges10 silver badges23 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

_lp_course was used in the older version of LearnPress. In LearnPress 4.x the relationship between lessons and courses is stored in a different way.

To find the course ID from a lesson ID, use : learn_press_get_course_id_by_lesson_id($lesson_id)

$course_id = learn_press_get_course_id_by_lesson_id($lesson_id);

after getting course ID you can mark the lesson completed using following code :

$user = learn_press_get_user($user_id);
if ($user) {
    $user->finish_lesson($lesson_id);
}

To get the course id from the lesson id you can try the below query. LearnPress uses this query. Check here

$output = $wpdb->get_var(
    $wpdb->prepare(
      "SELECT c.ID FROM {$wpdb->posts} c
      INNER JOIN {$wpdb->learnpress_sections} s ON c.ID = s.section_course_id
      INNER JOIN {$wpdb->learnpress_section_items} si ON si.section_id = s.section_id
      WHERE si.item_id = %d ORDER BY si.section_id DESC LIMIT 1
      ",
      $lesson_id
    )
);

But I think a better option to programmatically mark the lesson as completed is to use the Rest API. You just need to call the /wp-json/learnpress/v1/lessons/finish end point and pass the lesson id.

本文标签: phpProgrammatically mark lesson as complete in learnpressStack Overflow