# LearnDash: Last Learning Activity Column in Users

> Records when each student last completed a lesson, topic or quiz and shows it in a sortable "Last learning activity" column on the Users screen.

- Type: PHP
- Runs: Run everywhere
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: learndash, users, reporting
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/learndash-last-activity-users-column/

## Code

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

$wpide_ld_touch = function ( $data ) {
    $user_id = isset( $data['user']->ID ) ? (int) $data['user']->ID : get_current_user_id();

    if ( $user_id ) {
        update_user_meta( $user_id, 'wpide_ld_last_activity', time() );
    }
};

add_action( 'learndash_lesson_completed', $wpide_ld_touch );
add_action( 'learndash_topic_completed', $wpide_ld_touch );
add_action( 'learndash_quiz_completed', function ( $quizdata, $user ) {
    if ( $user instanceof WP_User ) {
        update_user_meta( $user->ID, 'wpide_ld_last_activity', time() );
    }
}, 10, 2 );

add_filter( 'manage_users_columns', function ( $columns ) {
    $columns['wpide_ld_activity'] = 'Last learning activity';

    return $columns;
} );

add_filter( 'manage_users_sortable_columns', function ( $columns ) {
    $columns['wpide_ld_activity'] = 'wpide_ld_activity';

    return $columns;
} );

add_filter( 'manage_users_custom_column', function ( $output, $column, $user_id ) {
    if ( 'wpide_ld_activity' !== $column ) {
        return $output;
    }

    $time = (int) get_user_meta( $user_id, 'wpide_ld_last_activity', true );

    return $time ? esc_html( sprintf( '%s ago', human_time_diff( $time ) ) ) : '&mdash;';
}, 10, 3 );

add_action( 'pre_get_users', function ( $query ) {
    if ( is_admin() && 'wpide_ld_activity' === $query->get( 'orderby' ) ) {
        $query->set( 'meta_key', 'wpide_ld_last_activity' );
        $query->set( 'orderby', 'meta_value_num' );
    }
} );
```

## Notes

Activity is recorded from the moment the snippet is active; earlier activity is not back-filled.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LearnDash: Last Learning Activity Column in Users" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
