You need to enable JavaScript to run this app. LearnDash中如何通过编程方式实现课程手动标记完成功能?
优惠活动
大模型
产品
解决方案
定价
更多

LearnDash中如何通过编程方式实现课程手动标记完成功能?

LearnDash 手动标记课程完成功能修复方案

核心问题说明

  • 你调用learndash_process_mark_complete函数时参数传递错误,第四个参数为课时ID,不需要传入课程ID
  • 前端传入的自定义完成日期没有落地存储,默认会使用函数执行时间作为完成时间
  • 没有做参数非空校验,参数缺失时会直接执行逻辑导致报错

修复后代码

前端JS代码

jQuery(document).on('click', '.course_complete_save', function(event) {
    event.preventDefault();

    var selected_group_id = jQuery('#group_name_select_dropdown').val();
    var selected_user_id = jQuery('#user_name_select_dropdown').val();
    var selected_course_id = jQuery('#course_name_select_dropdown').val();
    var mark_complete_date = jQuery('#input_date_field').val();

    // 新增基础参数校验
    if(!selected_user_id || !selected_course_id || !mark_complete_date) {
        jQuery(".mark_complete_success").empty().append("请完善必选字段");
        return false;
    }

    var params =  {
        "mark_complete_date": mark_complete_date,
        "selected_group_id": selected_group_id,
        "selected_user_id": selected_user_id,
        "selected_course_id": selected_course_id,
        "action": "mark_complete_course_ajax"
    }

    jQuery.post(groupcustomisation.ajaxurl, params, function(data){
        if(data == 'success'){
            jQuery(".mark_complete_success").empty().append("标记完成");
        }else if(data == 'empty_param'){
            jQuery(".mark_complete_success").empty().append("参数缺失,请重试");
        }else{
            jQuery(".mark_complete_success").empty().append("操作失败,请重试");
        }
    });
});

后端PHP代码

add_action('wp_ajax_nopriv_mark_complete_course_ajax', 'mark_complete_course_ajax');
add_action('wp_ajax_mark_complete_course_ajax', 'mark_complete_course_ajax');
function mark_complete_course_ajax() {
    // 新增参数校验与安全过滤
    if(empty($_POST['selected_user_id']) || empty($_POST['selected_course_id']) || empty($_POST['mark_complete_date'])) {
        echo 'empty_param';
        die();
    }

    $selected_group_id = absint($_POST['selected_group_id']);
    $selected_user_id = absint($_POST['selected_user_id']);
    $selected_course_id = absint($_POST['selected_course_id']);
    $mark_complete_date = sanitize_text_field($_POST['mark_complete_date']);
    // 把日期转成时间戳,可根据前端日期格式调整解析逻辑
    $complete_timestamp = strtotime($mark_complete_date);

    // 修正函数调用参数,仅需传入用户ID、课程ID、强制标记开关
    $complete_status = learndash_process_mark_complete( $selected_user_id, $selected_course_id, true );

    if($complete_status) {
        // 存储自定义完成时间,覆盖LearnDash默认生成的时间
        update_user_meta($selected_user_id, 'course_completed_' . $selected_course_id, $complete_timestamp);
        echo 'success';
    } else {
        echo 'fail';
    }
         
    die();
}

额外注意事项

  • 若前端使用自定义日期格式,可以用DateTime::createFromFormat替换strtotime做日期解析,避免解析错误
  • 确保你已经通过wp_localize_script方法把groupcustomisation.ajaxurl正确注入到前端,否则AJAX请求会报地址错误
  • 建议增加权限校验逻辑,确认操作的小组负责人有对应小组的管理权限,避免越权操作

内容的提问来源于stack exchange,提问作者Parthavi Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 08:00:04