bp_ld_sync/learndash_group_admin_added
Fires when a user is added as an administrator to a LearnDash group via BuddyPress.
add_action( 'bp_ld_sync/learndash_group_admin_added', $callback, 10, 2 );
Description
Fires when a user is added as an administrator to a LearnDash group managed by BuddyBoss. Developers can use this hook to perform custom actions when a group admin is assigned, such as sending notifications or updating user roles. Note that both the group ID and user ID are passed to the hook.
Usage
add_action( 'bp_ld_sync/learndash_group_admin_added', 'your_function_name', 10, 2 );
Parameters
-
$group_id(mixed) - The ID of the BuddyPress group.
-
$user_id(mixed) - The ID of the LearnDash group.
Examples
/**
* Sync Learndash group leader role to BuddyBoss group admin role.
*
* This function is triggered when a user is added as a leader to a LearnDash group.
* It then adds that user as an administrator to the corresponding BuddyBoss group.
*
* @param int $group_id The ID of the LearnDash group.
* @param int $user_id The ID of the user being added as a leader.
*/
function my_sync_learndash_group_leader_to_buddyboss_admin( $group_id, $user_id ) {
// Ensure LearnDash and BuddyBoss plugins are active and the IDs are valid.
if ( ! class_exists( 'LearnDash_Group_Permissions' ) || ! function_exists( 'groups_add_member' ) ) {
return;
}
if ( ! is_numeric( $group_id ) || ! is_numeric( $user_id ) ) {
return;
}
// You might want to have a way to map LearnDash groups to BuddyBoss groups.
// For this example, let's assume a direct correlation for simplicity.
// In a real-world scenario, you'd likely have a custom meta field or
// a dedicated mapping table.
$buddyboss_group_id = $group_id; // Assuming direct mapping
// Check if the BuddyBoss group exists.
$buddyboss_group = groups_get_group( array( 'group_id' => $buddyboss_group_id ) );
if ( ! $buddyboss_group ) {
// Log an error or notification if the BuddyBoss group doesn't exist.
error_log( "BuddyBoss group not found for LearnDash group ID: {$group_id}." );
return;
}
// Check if the user is already an admin of the BuddyBoss group.
if ( groups_is_user_admin( $user_id, $buddyboss_group_id ) ) {
return; // User is already an admin, no action needed.
}
// Add the user as an administrator to the BuddyBoss group.
$added = groups_add_member( $user_id, $buddyboss_group_id, 'admin' );
if ( is_wp_error( $added ) ) {
// Log an error if the user couldn't be added as an admin.
error_log( "Error adding user {$user_id} as admin to BuddyBoss group {$buddyboss_group_id}: " . $added->get_error_message() );
} else {
// Optional: Add a success notification or log.
// error_log( "User {$user_id} successfully added as admin to BuddyBoss group {$buddyboss_group_id}." );
}
}
add_action( 'bp_ld_sync/learndash_group_admin_added', 'my_sync_learndash_group_leader_to_buddyboss_admin', 10, 2 );
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:98
public function group_leader_added( $user_id, $group_id ) {
do_action( 'bp_ld_sync/learndash_group_admin_added', $group_id, $user_id );
}