Action uncanny-learndash-groups

bp_ld_sync/learndash_group_course_deleted

Fires when a LearnDash course is deleted from a BuddyPress group, passing group and course IDs.

add_action( 'bp_ld_sync/learndash_group_course_deleted', $callback, 10, 2 );

Description

Fires when a LearnDash course is removed from a BuddyBoss group. Developers can use this to perform custom actions, like revoking access or cleaning up related data, when a course is no longer associated with a specific group.


Usage

add_action( 'bp_ld_sync/learndash_group_course_deleted', 'your_function_name', 10, 2 );

Parameters

$group_id (mixed)
The ID of the BuddyPress group from which the course has been removed.
$course_id (mixed)
The ID of the BuddyBoss group from which the course was deleted.

Examples

add_action( 'bp_ld_sync/learndash_group_course_deleted', 'my_learndash_group_course_deleted_handler', 10, 2 );

/**
 * Handles the deletion of a LearnDash course from a BuddyBoss group.
 *
 * This function demonstrates how to react when a course is removed from a group,
 * potentially by revoking user access to the course if they only had access
 * through that specific group.
 *
 * @param int $group_id The ID of the BuddyBoss group.
 * @param int $course_id The ID of the LearnDash course.
 */
function my_learndash_group_course_deleted_handler( $group_id, $course_id ) {
	// Check if LearnDash and BuddyBoss plugins are active.
	if ( ! class_exists( 'SFW_LMS' ) || ! class_exists( 'BuddyBossCoreBuddyBoss_Core' ) ) {
		return;
	}

	// Get group members.
	$group_members = groups_get_members(
		array(
			'group_id' => $group_id,
			'fields'   => 'all',
		)
	);

	if ( ! empty( $group_members['members'] ) ) {
		foreach ( $group_members['members'] as $member ) {
			$user_id = $member->user_id;

			// Check if the user has access to the course through other means.
			// This is a simplified check; a more robust implementation might
			// involve checking group memberships across other related groups,
			// or direct course enrollment.
			$user_has_other_access = false;

			// Example: Check if the user is enrolled in the course directly.
			if ( function_exists( 'ld_is_user_enrolled' ) && ld_is_user_enrolled( $user_id, $course_id ) ) {
				$user_has_other_access = true;
			}

			// If the user doesn't have other access, consider revoking it.
			if ( ! $user_has_other_access ) {
				// Revoke access to the course for this user.
				// This might involve WordPress user meta or a specific function
				// provided by LearnDash or BuddyBoss for access management.
				// For demonstration, we'll simulate logging this action.
				error_log( sprintf( 'Revoking access for user %d to course %d due to removal from group %d.', $user_id, $course_id, $group_id ) );

				// Example: If LearnDash has a function like ld_revoke_course_access( $user_id, $course_id ), use it here.
				// For this example, we'll assume a hypothetical function or manual process.
				// If you are using LearnDash built-in group enrollment, this might be handled automatically.
			}
		}
	}

	// Log the event for debugging or auditing.
	error_log( sprintf( 'LearnDash course %d removed from BuddyBoss group %d.', $course_id, $group_id ) );
}

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/classes/buddyboss/buddy-boss-sync.php:138

public function course_removed_from_group( $course_id, $group_id ) {
		do_action( 'bp_ld_sync/learndash_group_course_deleted', $group_id, $course_id );
	}


Scroll to Top