Action uncanny-learndash-groups

bp_ld_sync/learndash_group_course_added

Fires when a LearnDash course is added to a BuddyPress group, passing the group and course IDs.

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

Description

Fires after a LearnDash course is successfully added to a BuddyBoss Group. Developers can use this hook to trigger custom logic, such as sending notifications, updating user metadata, or performing other integrations when a course becomes associated with a group. Ensure your callback function handles both the `$group_id` and `$course_id` parameters.


Usage

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

Parameters

$group_id (mixed)
The ID of the BuddyBoss group to which a LearnDash course has been added.
$course_id (mixed)
The ID of the BuddyPress group to which a LearnDash course has been added.

Examples

// Example of adding an action to the 'bp_ld_sync/learndash_group_course_added' hook.
// This function will run whenever a LearnDash course is added to a BuddyBoss group.
add_action( 'bp_ld_sync/learndash_group_course_added', function( $group_id, $course_id ) {

    // Check if the current user has the capability to manage courses or groups.
    // This is a basic security check to ensure only authorized users can perform certain actions.
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    // Get the BuddyBoss group object.
    $group = groups_get_group( array( 'group_id' => $group_id ) );

    // Check if the group exists.
    if ( ! $group ) {
        error_log( "BuddyBoss Group not found for group_id: {$group_id}" );
        return;
    }

    // Get the LearnDash course object.
    $course = get_post( $course_id );

    // Check if the course exists.
    if ( ! $course || $course->post_type !== 'sfwd-courses' ) {
        error_log( "LearnDash Course not found or invalid post type for course_id: {$course_id}" );
        return;
    }

    // Example: Add a custom meta field to the group indicating the course has been added.
    // This could be used for reporting or custom integrations.
    $course_ids_meta = get_post_meta( $group_id, '_learndash_courses_added', true );
    if ( empty( $course_ids_meta ) || ! is_array( $course_ids_meta ) ) {
        $course_ids_meta = array();
    }

    if ( ! in_array( $course_id, $course_ids_meta ) ) {
        $course_ids_meta[] = $course_id;
        update_post_meta( $group_id, '_learndash_courses_added', $course_ids_meta );

        // Example: Log that the course was successfully linked to the group.
        error_log( sprintf(
            "LearnDash course '%s' (ID: %d) successfully added to BuddyBoss group '%s' (ID: %d).",
            $course->post_title,
            $course_id,
            $group->name,
            $group_id
        ) );
    }

}, 10, 2 ); // Priority 10, accepting 2 arguments.

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:130

public function course_added_to_group( $course_id, $group_id ) {
		do_action( 'bp_ld_sync/learndash_group_course_added', $group_id, $course_id );
	}


Scroll to Top