# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: learndash, groups, email
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/learndash-notify-group-leaders/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LearnDash: Email Group Leaders When Their Students Finish a Course" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
