Action uncanny-automator

automator_learndash_lesson_completed

Fires when a user completes a LearnDash lesson, providing access to the user object.

add_action( 'automator_learndash_lesson_completed', $callback, 10, 1 );

Description

Fires when a user completes a LearnDash lesson. Developers can use this to trigger custom actions, such as sending notifications or awarding points. The hook provides access to the completed lesson ID and the user object. Ensure your callback function handles potential edge cases where LearnDash data might not be immediately available.


Usage

add_action( 'automator_learndash_lesson_completed', 'your_function_name', 10, 1 );

Parameters

$user (mixed)
This parameter contains the WordPress `WP_User` object for the user who completed the lesson.

Examples

<?php
/**
 * Example of a WordPress action hook for LearnDash lesson completion.
 *
 * This function demonstrates how to hook into the 'automator_learndash_lesson_completed'
 * action to perform custom logic when a user completes a LearnDash lesson.
 * In this example, we're logging the completion to a custom user meta field.
 */
add_action(
	'automator_learndash_lesson_completed',
	function ( $args ) {
		// Ensure we have the necessary arguments.
		if ( ! isset( $args['user'] ) || ! isset( $args['lesson'] ) ) {
			return;
		}

		$user = $args['user']; // The WordPress User object.
		$lesson = $args['lesson']; // The WordPress Post object for the lesson.

		// Get the user's ID.
		$user_id = $user->ID;

		// Get the lesson ID.
		$lesson_id = $lesson->ID;

		// Get the course ID if available.
		$course_id = isset( $args['course'] ) ? $args['course']->ID : null;

		// Get the current timestamp.
		$completion_timestamp = current_time( 'mysql' );

		// Construct the data to save.
		$completion_data = array(
			'lesson_id' => $lesson_id,
			'lesson_title' => get_the_title( $lesson_id ),
			'completed_at' => $completion_timestamp,
		);

		if ( $course_id ) {
			$completion_data['course_id'] = $course_id;
			$completion_data['course_title'] = get_the_title( $course_id );
		}

		// Retrieve existing lesson completion data for the user.
		$user_lesson_completions = get_user_meta( $user_id, 'learndash_lesson_completions', true );

		// Initialize if not set.
		if ( ! is_array( $user_lesson_completions ) ) {
			$user_lesson_completions = array();
		}

		// Add the new completion data, ensuring we don't have duplicates for the same lesson.
		$user_lesson_completions[ $lesson_id ] = $completion_data;

		// Save the updated lesson completion data back to user meta.
		update_user_meta( $user_id, 'learndash_lesson_completions', $user_lesson_completions );

		// You could also log this or trigger other actions here.
		// For example, sending an email notification.
		// error_log( "User {$user->user_login} completed lesson {$lesson->post_title}." );

	},
	10, // Priority
	1  // Accepted args (the $args array)
);
?>

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/learndash/helpers/learndash-helpers.php:968
src/integrations/learndash/helpers/learndash-helpers.php:984

public function learndash_update_user_activity_func( $args ) {
		// Bail early if args is empty
		if ( empty( $args ) ) {
			return;
		}
		// If it's not an admin (or ajax for quiz complete), bail
		if ( function_exists( 'is_admin' ) && ! is_admin() ) {
			return;
		}
		// activity status is empty or not completed, bail
		if ( ! isset( $args['activity_status'] ) || 1 !== absint( $args['activity_status'] ) ) {
			return;
		}
		// 'update' action is called when an activity is updated
		$activity_action = $args['activity_action'];
		if ( 'update' !== $activity_action && 'insert' !== $activity_action ) {
			return;
		}
		// if activity_completed timestamp is empty, bail
		if ( empty( $args['activity_completed'] ) ) {
			return;
		}
		$user_id         = absint( $args['user_id'] );
		$user            = get_user_by( 'ID', $user_id );
		$post_id         = absint( $args['post_id'] ); // Course, lesson or topic ID
		$course_id       = absint( $args['course_id'] ); // Linked Course ID
		$activity_type   = $args['activity_type']; // course, lesson or topic
		$course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true ); // course progress
		// Activity type is lesson, fire do_action
		if ( 'lesson' === $activity_type ) {
			do_action(
				'automator_learndash_lesson_completed',
				array(
					'user'     => $user,
					'course'   => get_post( $course_id ),
					'lesson'   => get_post( $post_id ),
					'progress' => $course_progress,
				)
			);

			return;
		}

		// Activity type is topic, fire do_action
		if ( 'topic' === $activity_type ) {
			$lesson_id = learndash_get_lesson_id( $post_id, $course_id );
			do_action(
				'automator_learndash_lesson_completed',
				array(
					'user'     => $user,
					'course'   => get_post( $course_id ),
					'lesson'   => get_post( $lesson_id ),
					'topic'    => get_post( $post_id ),
					'progress' => $course_progress,
				)
			);

			return;
		}
	}

Scroll to Top