You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LearnDash中跨课程同步共享Topic的完成状态

LearnDash共享Topic完成状态跨课程同步问题

我已将LearnDash配置为共享课程步骤,可在多门课程中复用相同的Lesson和Topic。但默认设置下,用户在某一课程中标记Topic为完成后,该状态不会同步到其他包含此Topic的课程。我需要实现共享Topic的完成状态变更能同步至所有包含它的课程,但使用以下代码后并未生效:

function nzf_sync_topic_completion($topic_data) {
    // Extract relevant data
    $topic_id = isset($topic_data['topic']->ID) ? $topic_data['topic']->ID : null;
    $course_id = isset($topic_data['course']->ID) ? $topic_data['course']->ID : 
    $user_id   = isset($topic_data['user']->ID) ? $topic_data['user']->ID : null;

    // Validate that essential data exists
    if (!$topic_id || !$course_id || !$user_id) {
        error_log('nzf_sync_topic_completion: Missing required data. Topic, course, 
        return;
    }

    // Check if LearnDash shared steps is enabled
    if (learndash_is_course_shared_steps_enabled()) {
        // Get all courses sharing the topic
        $shared_courses = learndash_get_courses_for_step($topic_id, true);

        // Loop through shared courses
        foreach ($shared_courses as $shared_course_id => $value) {
            // Skip the current course
            if ($shared_course_id == $course_id) {
                continue;
            }

            // Mark the topic as complete for the user in the shared course
            learndash_process_mark_complete($user_id, $topic_id, true, 
        }
    } else {
        error_log('nzf_sync_topic_completion: Shared steps not enabled.');
    }


// Hook into the LearnDash topic completion action
add_action('learndash_topic_completed', 'nzf_sync_topic_completion', 10, 1);

问题分析与修正代码

原代码存在多处语法错误和参数缺失,导致无法正常执行:

  1. 变量赋值语句未闭合($course_id行)
  2. 错误日志字符串未闭合
  3. learndash_process_mark_complete函数参数不完整,缺少目标课程ID
  4. 函数未正确闭合

以下是修正后的完整代码:

function nzf_sync_topic_completion($topic_data) {
    // 提取关键数据
    $topic_id = isset($topic_data['topic']->ID) ? $topic_data['topic']->ID : null;
    $course_id = isset($topic_data['course']->ID) ? $topic_data['course']->ID : null;
    $user_id = isset($topic_data['user']->ID) ? $topic_data['user']->ID : null;

    // 验证必要数据是否存在
    if (!$topic_id || !$course_id || !$user_id) {
        error_log('nzf_sync_topic_completion: 缺少必要数据(Topic、课程或用户ID)');
        return;
    }

    // 检查共享课程步骤是否启用
    if (learndash_is_course_shared_steps_enabled()) {
        // 获取所有包含该Topic的共享课程
        $shared_courses = learndash_get_courses_for_step($topic_id, true);

        // 遍历共享课程
        foreach ($shared_courses as $shared_course_id => $value) {
            // 跳过当前触发完成的课程
            if ($shared_course_id == $course_id) {
                continue;
            }

            // 标记该用户在共享课程中的Topic为已完成
            // 补充完整参数:用户ID、步骤ID、是否完成、目标课程ID
            learndash_process_mark_complete($user_id, $topic_id, true, $shared_course_id);
        }
    } else {
        error_log('nzf_sync_topic_completion: 共享课程步骤未启用');
    }
}
// 挂钩到LearnDash的Topic完成事件
add_action('learndash_topic_completed', 'nzf_sync_topic_completion', 10, 1);
关键修改点
  • 修复所有语法错误,确保代码能正常解析执行
  • 为learndash_process_mark_complete补充第四个参数$shared_course_id,这是将完成状态关联到目标共享课程的必要参数
  • 优化错误日志提示,便于排查问题
  • 确保函数正确闭合,挂钩逻辑位于函数定义外部

内容的提问来源于stack exchange,提问作者Carmi Human

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 21:04:53