Tutor LMS: Email the Admin When a Course Is Completed
Emails you whenever a student completes a Tutor LMS course, with the student's name, email and the course.
Opens WPIDE on
Code
if ( ! function_exists( 'tutor_utils' ) ) {
return;
}
$wpide_tutor_notify_emails = array( get_option( 'admin_email' ) );
// Also email the course author (the main instructor).
$wpide_tutor_notify_author = true;
add_action( 'tutor_course_complete_after', function ( $course_id, $user_id = 0 ) use ( $wpide_tutor_notify_emails, $wpide_tutor_notify_author ) {
$user = get_userdata( $user_id ? (int) $user_id : get_current_user_id() );
if ( ! $user ) {
return;
}
$to = $wpide_tutor_notify_emails;
if ( $wpide_tutor_notify_author ) {
$author = get_userdata( (int) get_post_field( 'post_author', $course_id ) );
if ( $author ) {
$to[] = $author->user_email;
}
}
wp_mail(
array_unique( $to ),
sprintf( 'Course completed: %s', get_the_title( $course_id ) ),
sprintf( "%s (%s) completed \"%s\" on %s.", $user->display_name, $user->user_email, get_the_title( $course_id ), wp_date( get_option( 'date_format' ) ) )
);
}, 10, 2 );
Notes
Add more addresses to $wpide_tutor_notify_emails; the course instructor can be added with the option below.
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.