功能简介
- 自动记录每篇文章访客信息(未登录游客除外)
- 后台可设置头像大小(根据头像大小容纳更多访客)
效果展示
部署教程:
将下方代码写入 func.php 或 functions.php 文件,下面两个都一样,下面的压缩了一下
// 记录访客 - dahai
add_action('wp_head', 'dahai_record_visitor');
function dahai_record_visitor() {
if (!is_singular('post') || !is_user_logged_in()) return;
global $post;
$visitors = get_post_meta($post->ID, 'dahai_visitors', true) ?: [];
$uid = get_current_user_id();
$visitors = array_filter($visitors, fn($v) => $v != $uid);
array_unshift($visitors, $uid);
update_post_meta($post->ID, 'dahai_visitors', array_slice($visitors, 0, 50));
}
// 注册小工具 - dahai
add_action('widgets_init', fn() => register_widget('Dahai_Visitor_Widget'));
class Dahai_Visitor_Widget extends WP_Widget {
function __construct() {
parent::__construct('dahai_visitor_widget', '文章访客 - Dahai', ['description' => '显示文章访客 by Dahai']);
}
function widget($args, $instance) {
if (!is_singular('post')) return;
global $post;
$visitors = get_post_meta($post->ID, 'dahai_visitors', true);
if (empty($visitors)) return;
$num = $instance['num'] ?? 12;
$badge = $instance['badge'] ?? '最近访客';
$pos = $instance['pos'] ?? 'right';
$size = $instance['size'] ?? 50;
$fontSize = $instance['fontSize'] ?? 0.9;
$pStyle = $pos == 'left' ? 'left:-50px;transform:rotate(-45deg);' : 'right:-50px;transform:rotate(45deg);';
echo '<div class="theme-box dahai-visitor-box" style="position:relative;overflow:hidden;margin-bottom:0;">';
echo '<span class="dahai-badge" style="position:absolute;top:10px;' . $pStyle . 'z-index:1;width:140px;height:20px;background:#2c40a0;color:#fff;line-height:20px;text-align:center;font-size:12px;">' . esc_html($badge) . '</span>';
echo '<div class="zib-widget dahai-visitor-grid" style="padding:15px;display:grid;grid-template-columns:repeat(auto-fill,minmax(' . $size . 'px,1fr));gap:15px;">';
foreach (array_slice($visitors, 0, $num) as $uid) {
$user = get_userdata($uid);
if (!$user) continue;
// 获取头像
$avatar_img = zib_get_data_avatar($uid, $size);
// 获取会员徽章并根据头像尺寸调整大小
$vip_level = zib_get_user_vip_level($uid);
$vip_badge = '';
if ($vip_level) {
$vip_img_src = zibpay_get_vip_icon_img_url($vip_level);
$badge_size = round($size * 0.35); // 徽章尺寸为头像的35%
$vip_badge = '<img class="avatar-vip-icon" src="' . $vip_img_src . '" style="position:absolute;right:0;bottom:0;width:' . $badge_size . 'px;height:' . $badge_size . 'px;border-radius:50%;">';
}
echo '<div class="dahai-visitor-item" style="text-align:center;"><a href="' . zib_get_user_home_url($uid) . '" rel="external nofollow" style="display:block;">';
echo '<div class="dahai-avatar" style="display:flex;justify-content:center;"><span class="avatar-img" style="position:relative;width:' . $size . 'px;height:' . $size . 'px;display:inline-block;">' . $avatar_img . $vip_badge . '</span></div>';
echo '<div class="dahai-username" style="margin-top:6px;font-size:' . $fontSize . 'em;color:var(--muted-color);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:100%;">' . esc_html($user->display_name) . '</div>';
echo '</a></div>';
}
echo '</div></div>';
}
function form($instance) {
$badge = $instance['badge'] ?? '最近访客';
$pos = $instance['pos'] ?? 'right';
$num = $instance['num'] ?? 12;
$size = $instance['size'] ?? 50;
$fontSize = $instance['fontSize'] ?? 0.9;
?>
<p><label>角标:<input class="widefat" name="<?= $this->get_field_name('badge') ?>" value="<?= esc_attr($badge) ?>"></label></p>
<p><label>位置:<select class="widefat" name="<?= $this->get_field_name('pos') ?>">
<option value="right" <?php selected($pos, 'right') ?>>右上</option>
<option value="left" <?php selected($pos, 'left') ?>>左上</option>
</select></label></p>
<p><label>数量:<input class="widefat" type="number" name="<?= $this->get_field_name('num') ?>" value="<?= esc_attr($num) ?>" min="1" max="50"></label></p>
<p><label>头像尺寸(px):<input class="widefat" type="number" name="<?= $this->get_field_name('size') ?>" value="<?= esc_attr($size) ?>" min="30" max="100" step="5"></label><small style="color:#666;">范围:30-80</small></p>
<p><label>用户名字号(em):<input class="widefat" type="number" name="<?= $this->get_field_name('fontSize') ?>" value="<?= esc_attr($fontSize) ?>" min="0.5" max="2" step="0.1"></label><small style="color:#666;">范围:0.5-2</small></p>
<?php
}
function update($new, $old) {
return [
'badge' => strip_tags($new['badge']),
'pos' => in_array($new['pos'], ['left', 'right']) ? $new['pos'] : 'right',
'num' => absint($new['num']),
'size' => max(30, min(80, absint($new['size']))),
'fontSize' => max(0.5, min(2, floatval($new['fontSize'])))
];
}
}

