子比文章最近访客统计小工具

2026-02-14 0 900

功能简介

  • 自动记录每篇文章访客信息(未登录游客除外)
  • 后台可设置头像大小(根据头像大小容纳更多访客)

效果展示

子比文章最近访客统计小工具

部署教程:

将下方代码写入 func.php 或 functions.php 文件,下面两个都一样,下面的压缩了一下

[hidecontent type=”payshow” desc=”隐藏内容:付费阅读”]

// 记录访客 - 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'])))
        ];
    }
}

 

[/hidecontent]

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

轩玮博客 子比主题 子比文章最近访客统计小工具 http://blog.xuwbk.com/1226.html

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务