LearnDash: Email Group Leaders When Their Students Finish a Course
Notifies the leaders of each LearnDash group a student belongs to when that student completes a course, so managers can follow their team's training.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_action( 'learndash_course_completed', function ( $data ) {
if ( empty( $data['user'] ) || empty( $data['course'] ) || ! function_exists( 'learndash_get_users_group_ids' ) ) {
return;
}
$leaders = array();
foreach ( (array) learndash_get_users_group_ids( $data['user']->ID ) as $group_id ) {
$leaders = array_merge( $leaders, (array) learndash_get_groups_administrator_ids( $group_id ) );
}
$leaders = array_diff( array_unique( array_map( 'intval', $leaders ) ), array( (int) $data['user']->ID ) );
foreach ( $leaders as $leader_id ) {
$leader = get_userdata( $leader_id );
if ( ! $leader ) {
continue;
}
wp_mail(
$leader->user_email,
sprintf( '%s completed %s', $data['user']->display_name, get_the_title( $data['course'] ) ),
sprintf( "Hi %s,\n\n%s from your group has completed \"%s\".", $leader->display_name, $data['user']->display_name, get_the_title( $data['course'] ) )
);
}
} );
Notes
Leaders get one email per completion, even if they lead several of the student's groups.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.