Learndash通过action hook对接Make触发Webhook报错求助
解决Learndash课时完成触发Webhook时的PHP致命错误
错误原因
你遇到的ArgumentCountError是因为**learndash_lesson_completed钩子实际会传递6个参数**,但你的函数只定义了2个参数,且add_action中声明接收2个参数,导致PHP触发参数数量不匹配的致命错误。
修正后的代码
function my_custom_lesson_completed_action( $lesson_id, $user_id, $course_id, $quiz_id, $lesson_data, $user_meta ) { // 验证用户和课时数据有效性 $user = get_user_by( 'ID', $user_id ); $lesson = get_post( $lesson_id ); if ( !$user || !$lesson ) { error_log( 'Invalid user or lesson data for lesson completion webhook' ); return; } // 准备Webhook请求数据 $payload = array( 'user_id' => $user_id, 'user_email' => $user->user_email, 'user_display_name' => $user->display_name, 'lesson_id' => $lesson_id, 'lesson_title' => $lesson->post_title, 'course_id' => $course_id, 'completed_timestamp' => current_time( 'timestamp' ) ); // 替换为你的Make Webhook地址 $webhook_url = 'https://hook.integromat.com/your_webhook_url'; // 发送POST请求 $response = wp_remote_post( $webhook_url, array( 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $payload ), 'timeout' => 10 ) ); // 记录请求结果 if ( is_wp_error( $response ) ) { error_log( 'Lesson completion webhook failed: ' . $response->get_error_message() ); } else { error_log( 'Lesson completion webhook sent successfully for user ' . $user_id . ', lesson ' . $lesson_id ); } } // 注册钩子时声明接收6个参数,与钩子实际传递数量匹配 add_action( 'learndash_lesson_completed', 'my_custom_lesson_completed_action', 10, 6 );
关键修改点
- 参数匹配:将函数参数调整为6个,同时把
add_action的第四个参数(接收参数数量)从2改为6,与钩子实际传递的参数数量保持一致。 - 数据校验:增加用户和课时数据的有效性判断,避免因数据为空导致后续代码报错。
- 日志增强:补充失败和成功状态的日志记录,方便后续排查问题。
- 细节优化:添加超时设置、用户昵称、关联课程ID等字段,提升Webhook数据的实用性。
注意事项
- 务必将
$webhook_url替换为你在Make平台生成的实际Webhook地址。 - 不需要的参数可以保留占位但不能删除,否则仍会触发参数数量错误。
- 建议通过Code Snippets插件或子主题的
functions.php添加代码,避免修改Learndash核心文件。
内容的提问来源于stack exchange,提问作者E E
相关产品推荐
相关产品推荐

