LearnDash: Email the Admin When a Course Is Completed
Emails you (or a list of addresses) whenever a student completes a LearnDash course, with the student's name and a link to their profile.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
$wpide_ld_notify_emails = array( get_option( 'admin_email' ) );
// Leave empty for all courses.
$wpide_ld_notify_courses = array();
add_action( 'learndash_course_completed', function ( $data ) use ( $wpide_ld_notify_emails, $wpide_ld_notify_courses ) {
if ( empty( $data['user'] ) || empty( $data['course'] ) ) {
return;
}
if ( $wpide_ld_notify_courses && ! in_array( (int) $data['course']->ID, $wpide_ld_notify_courses, true ) ) {
return;
}
wp_mail(
$wpide_ld_notify_emails,
sprintf( 'Course completed: %s', get_the_title( $data['course'] ) ),
sprintf(
"%s (%s) completed \"%s\" on %s.\n\nProfile: %s",
$data['user']->display_name,
$data['user']->user_email,
get_the_title( $data['course'] ),
wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
admin_url( 'user-edit.php?user_id=' . (int) $data['user']->ID )
)
);
} );
Notes
Add more addresses to $wpide_ld_notify_emails, or limit it to some courses with $wpide_ld_notify_courses.
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.