LifterLMS: Dashboard Widget Listing the Latest Graduates
Keeps a short log of the latest LifterLMS course completions and shows it as a dashboard widget for admins and instructors.
Opens WPIDE on
Code
if ( ! function_exists( 'llms_get_student' ) ) {
return;
}
add_action( 'lifterlms_course_completed', function ( $user_id, $course_id ) {
$log = (array) get_option( 'wpide_llms_completions', array() );
array_unshift( $log, array( 'user' => (int) $user_id, 'course' => (int) $course_id, 'time' => time() ) );
update_option( 'wpide_llms_completions', array_slice( $log, 0, 15 ), false );
}, 10, 2 );
add_action( 'wp_dashboard_setup', function () {
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_llms_completions', 'Recent course completions', function () {
$log = array_filter( (array) get_option( 'wpide_llms_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 %2$s <span style="color:#646970">%3$s ago</span></li>',
esc_html( $user ? $user->display_name : 'Deleted user' ),
esc_html( get_the_title( $row['course'] ) ),
esc_html( human_time_diff( (int) $row['time'] ) )
);
}
echo '</ul>';
} );
} );
Notes
The log starts when you activate the snippet and holds the last 15 completions.
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.