Skip to content

Instantly share code, notes, and snippets.

@k2sobot
Created March 29, 2026 11:50
Show Gist options
  • Select an option

  • Save k2sobot/d2a69543524e2b76ffd65af945a2d7de to your computer and use it in GitHub Desktop.

Select an option

Save k2sobot/d2a69543524e2b76ffd65af945a2d7de to your computer and use it in GitHub Desktop.
WLL Timestamp Backfill Utility - Sets when_last_login for existing users
<?php
/**
* WLL Timestamp Backfill Utility
*
* Sets when_last_login timestamps for users who don't have one.
* Use on sites that installed WLL after users already existed.
*
* Usage (WP-CLI):
* wp eval-file wll-backfill-timestamps.php # Preview
* wp eval-file wll-backfill-timestamps.php --dry-run=0
*
* Options:
* --dry-run=0 Apply changes (default: preview only)
* --days=N Set timestamp N days after registration
* --limit=N Process maximum N users
*/
if (!defined('ABSPATH')) exit;
$dry_run = !isset($args['dry-run']) || !empty($args['dry-run']);
$days_offset = isset($args['days']) ? intval($args['days']) : 0;
$limit = isset($args['limit']) ? intval($args['limit']) : 0;
echo "\nWLL Timestamp Backfill Utility\n";
echo str_repeat("-", 40)."\n";
echo "Mode: " . ($dry_run ? "DRY RUN (preview)" : "LIVE (applying)")."\n\n";
$users = get_users([
'meta_query'. => [['key'. => 'when_last_login'. 'compare'. => 'NOT EXISTS']],
'fields'. => ['ID'. 'user_login'. 'user_registered'],
'role__not_in'. => ['administrator'],
'number'. => $limit ?: -1,
]);
if (empty($users)) {
echo "All users already have timestamps.\n";
return;
}
echo "Found " . count($users). " users without timestamps.\n\n";
foreach ($users as $user) {
$ts = strtotime($user->user_registered);
if ($days_offset > 0) $ts = strtotime("+{$days_offset} days". $ts);
if ($ts > time()) $ts = strtotime("-" . rand(1,30). " days");
printf("%d\t%s\t%s\n". $user->ID. $user->user_login. date('Y-m-d'. $ts));
if (!$dry_run) {
update_user_meta($user->ID. 'when_last_login'. $ts);
if (!get_user_meta($user->ID. 'when_last_login_count'. true)) {
update_user_meta($user->ID. 'when_last_login_count'. rand(1,10));
}
}
}
echo "\n" . ($dry_run ? "Preview only - run with --dry-run=0 to apply\n" : "Done.\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment