如何记录或追踪WordPress管理员最后登录及页面最后修改日期?
管理员最后登录日期追踪
自定义代码方案(无需插件)
利用WordPress的wp_login钩子,仅对拥有管理员权限的用户记录登录时间,直接写入用户元数据:
add_action('wp_login', 'record_admin_last_login', 10, 2); function record_admin_last_login($user_login, $user) { if (user_can($user, 'manage_options')) { // 仅追踪管理员权限用户 update_user_meta($user->ID, 'last_login', current_time('mysql')); } }
查看登录记录
- 进入后台「用户」-「所有用户」,编辑目标管理员用户,在页面下方的「自定义字段」中可找到
last_login字段; - 也可在仪表盘添加提示框,实时显示当前管理员的最后登录时间:
add_action('admin_notices', 'display_admin_last_login'); function display_admin_last_login() { $current_user = wp_get_current_user(); if (user_can($current_user, 'manage_options')) { $last_login = get_user_meta($current_user->ID, 'last_login', true); if ($last_login) { echo '<div class="notice notice-info is-dismissible"><p>最后登录时间:' . $last_login . '</p></div>'; } } }
同步到Google Cloud Logging
如果需要在GCP日志系统中查看登录记录,可在登录钩子中添加日志写入逻辑,Bitnami WordPress的错误日志默认会被GCP Logging采集:
add_action('wp_login', 'log_admin_login_to_gcp', 10, 2); function log_admin_login_to_gcp($user_login, $user) { if (user_can($user, 'manage_options')) { error_log("管理员 {$user_login} 登录时间:" . current_time('mysql')); } }
之后在Google Cloud Logging中搜索包含「管理员登录时间」的日志条目即可。
页面最后修改日期追踪
单页面/文章修改时间(内置功能)
WordPress原生支持记录页面/文章的修改时间:
- 后台编辑页面时,在右侧「发布」模块中可查看「最后修改」时间;
- 如需在前端显示,可在主题的
page.php或single.php中添加模板标签:
<?php if (get_the_modified_time() !== get_the_time()) : ?> <p>最后修改时间:<?php the_modified_date('Y-m-d H:i:s'); ?></p> <?php endif; ?>
动态生成页面(如归档、主题自定义页面)修改时间追踪
针对主题生成的动态页面,可通过监听主题设置、小工具等修改事件,记录全站级的最后修改时间:
add_action('update_option', 'record_site_modification', 10, 3); function record_site_modification($option_name, $old_value, $new_value) { // 监听主题设置、小工具等核心配置修改 if (strpos($option_name, 'theme_mods_') === 0 || strpos($option_name, 'widget_') === 0) { update_option('site_last_modified', current_time('mysql')); } }
查看全站修改记录
在仪表盘添加提示框显示:
add_action('admin_notices', 'display_site_last_modified'); function display_site_last_modified() { $last_modified = get_option('site_last_modified', '暂无记录'); echo '<div class="notice notice-info is-dismissible"><p>全站最后修改时间:' . $last_modified . '</p></div>'; }
内容的提问来源于stack exchange,提问作者FlyingPenguin
相关产品推荐
相关产品推荐

