bp_ld_sync/learndash_group_user_added
Fires when a user is added to a LearnDash group, allowing for custom actions.
add_action( 'bp_ld_sync/learndash_group_user_added', $callback, 10, 2 );
Description
Fires when a user is added to a LearnDash group managed by BuddyBoss. Developers can use this to trigger custom actions or synchronize data between LearnDash and BuddyBoss upon user group enrollment. The hook passes the group ID and user ID as parameters.
Usage
add_action( 'bp_ld_sync/learndash_group_user_added', 'your_function_name', 10, 2 );
Parameters
-
$group_id(mixed) - The ID of the LearnDash group to which the user has been added.
-
$user_id(mixed) - The ID of the LearnDash group to which the user has been added.
Examples
/**
* Example: Enroll the user in a LearnDash course when they are added to a BuddyBoss group.
*
* This function hooks into the 'bp_ld_sync/learndash_group_user_added' action.
* When a user is added to a specific BuddyBoss group, this function will enroll
* that user into a predefined LearnDash course.
*/
add_action( 'bp_ld_sync/learndash_group_user_added', function( $group_id, $user_id ) {
// Define the BuddyBoss Group ID and the LearnDash Course ID you want to sync.
// Replace these with your actual IDs.
$target_buddyboss_group_id = 123; // Example BuddyBoss Group ID
$target_learndash_course_id = 456; // Example LearnDash Course ID
// Check if the current action is for the target BuddyBoss group.
if ( $group_id == $target_buddyboss_group_id ) {
// Check if the LearnDash plugin is active.
if ( class_exists( 'SFWD_LMS' ) ) {
// Enroll the user in the LearnDash course.
$course_id = learndash_enroll_user( $user_id, $target_learndash_course_id );
// Optionally, you can add a notification or log the action.
if ( $course_id ) {
// Successful enrollment. You might want to log this or notify the admin.
// Example: error_log( "User {$user_id} successfully enrolled in LearnDash course {$target_learndash_course_id} due to being added to BuddyBoss group {$group_id}." );
} else {
// Enrollment failed.
// Example: error_log( "Failed to enroll User {$user_id} in LearnDash course {$target_learndash_course_id} for BuddyBoss group {$group_id}." );
}
} else {
// LearnDash plugin is not active.
// Example: error_log( "LearnDash plugin is not active. Cannot enroll user {$user_id}." );
}
}
}, 10, 2 ); // 10 is the priority, 2 is the number of accepted 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:114
public function user_added_to_group( $user_id, $group_id ) {
do_action( 'bp_ld_sync/learndash_group_user_added', $group_id, $user_id );
}