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.
Opens WPIDE on
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
- 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.