Action uncanny-learndash-groups

bp_ld_sync/learndash_group_admin_removed

Fires when a LearnDash group administrator is removed from a BuddyPress group.

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

Description

Fires when a LearnDash group leader is removed from a BuddyBoss group. Developers can use this to trigger custom logic, such as revoking access to LearnDash content associated with the group or performing other cleanup tasks when a user's leadership role is revoked.


Usage

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

Parameters

$group_id (mixed)
This parameter contains the ID of the BuddyPress group from which the user was removed as an administrator.
$user_id (mixed)
The ID of the BuddyPress group from which the user was removed as an administrator.

Examples

add_action( 'bp_ld_sync/learndash_group_admin_removed', 'my_learndash_group_admin_removed_handler', 10, 2 );

/**
 * Handles the removal of a LearnDash group administrator.
 *
 * This function is triggered when a user is removed as an administrator from a BuddyBoss group
 * that is synced with a LearnDash group. It performs actions like revoking access to related
 * LearnDash courses or updating user meta.
 *
 * @param int $group_id The ID of the BuddyBoss group.
 * @param int $user_id  The ID of the user who was removed as an admin.
 */
function my_learndash_group_admin_removed_handler( $group_id, $user_id ) {
    // Check if the group_id and user_id are valid integers.
    if ( ! absint( $group_id ) || ! absint( $user_id ) ) {
        error_log( 'Invalid group ID or user ID passed to my_learndash_group_admin_removed_handler.' );
        return;
    }

    // Example: Revoke access to a specific LearnDash course if the group is associated.
    // In a real scenario, you'd likely have a meta key linking the BuddyBoss group to a LearnDash course.
    $learndash_course_id = get_post_meta( $group_id, '_learndash_course_id_association', true );

    if ( $learndash_course_id && absint( $learndash_course_id ) ) {
        // Check if the user is enrolled in this LearnDash course.
        if ( learndash_is_user_enrolled( $user_id, $learndash_course_id ) ) {
            // Revoke access to the course. This is a simplified example;
            // actual implementation might involve removing them from a specific group in LearnDash,
            // or triggering a different LearnDash API function.
            // For demonstration, let's assume we are un-enrolling them directly if this is the desired behavior.
            // Note: Direct un-enrollment might have consequences. Use with caution and consider LearnDash's API.

            // Example: using LearnDash's API to remove enrollment.
            // This is a hypothetical function, check LearnDash documentation for the correct API.
            // if ( function_exists( 'learndash_remove_user_from_course' ) ) {
            //     learndash_remove_user_from_course( $user_id, $learndash_course_id );
            //     error_log( "User {$user_id} un-enrolled from LearnDash course {$learndash_course_id} due to removal from BuddyBoss group {$group_id} admin." );
            // } else {
                 error_log( "User {$user_id} removed as admin from BuddyBoss group {$group_id}. Associated LearnDash course {$learndash_course_id} access logic would be applied here." );
            // }
        }
    }

    // Example: Update user meta to reflect removal from admin status for this group.
    // You might want to store which groups a user was an admin of for sync purposes.
    update_user_meta( $user_id, '_buddyboss_learndash_group_admin_of_' . $group_id, false );

    // You could also trigger notifications to the user or group administrators.
    // wp_mail( get_userdata( $user_id )->user_email, 'You are no longer a group admin', 'You have been removed as an administrator for group 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:106

public function group_leader_removed( $user_id, $group_id ) {
		do_action( 'bp_ld_sync/learndash_group_admin_removed', $group_id, $user_id );
	}


Scroll to Top