Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.