Halloween Sale 15% off Ends in See the plans
PHP

LearnDash: Recent Course Completions Dashboard Widget

Keeps a short log of the latest course completions and shows it in a dashboard widget with student names, courses and how long ago.

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

Code

if ( ! defined( 'LEARNDASH_VERSION' ) ) {
    return;
}

add_action( 'learndash_course_completed', function ( $data ) {
    if ( empty( $data['user'] ) || empty( $data['course'] ) ) {
        return;
    }

    $log = (array) get_option( 'wpide_ld_completions', array() );
    array_unshift( $log, array( 'user' => (int) $data['user']->ID, 'course' => (int) $data['course']->ID, 'time' => time() ) );
    update_option( 'wpide_ld_completions', array_slice( $log, 0, 15 ), false );
} );

add_action( 'wp_dashboard_setup', function () {
    if ( ! current_user_can( 'list_users' ) ) {
        return;
    }

    wp_add_dashboard_widget( 'wpide_ld_completions', 'Recent course completions', function () {
        $log = array_filter( (array) get_option( 'wpide_ld_completions', array() ), 'is_array' );

        if ( ! $log ) {
            echo '<p>No completions recorded yet.</p>';
            return;
        }

        echo '<ul>';

        foreach ( $log as $row ) {
            $user = get_userdata( $row['user'] );

            printf(
                '<li><strong>%1$s</strong> completed <a href="%2$s">%3$s</a> <span style="color:#646970">%4$s ago</span></li>',
                esc_html( $user ? $user->display_name : 'Deleted user' ),
                esc_url( get_edit_post_link( $row['course'] ) ),
                esc_html( get_the_title( $row['course'] ) ),
                esc_html( human_time_diff( (int) $row['time'] ) )
            );
        }

        echo '</ul>';
    } );
} );

Notes

The log starts when the snippet is activated and keeps the 15 most recent completions in one small option.

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.