首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[分享]WordPress 评论者信息显示IP地址以及操作平台信息

发布于 2024-11-04 16:59:10
0
122

要在WordPress评论部分显示评论者的IP地址和操作平台信息,可以通过以下代码实现:// 在评论部分显示评论者的IP地址和操作平台信息 function show_commenter_ip_and...

要在WordPress评论部分显示评论者的IP地址和操作平台信息,可以通过以下代码实现:

// 在评论部分显示评论者的IP地址和操作平台信息
function show_commenter_ip_and_platform($comment_text) {
    $comment_id = get_comment_ID();
    $comment = get_comment($comment_id);
    
    $commenter_ip = $comment->comment_author_IP;
    $commenter_agent = $comment->comment_agent;
    
    $platform = '';
    if (preg_match('/Windows/', $commenter_agent)) {
        $platform = 'Windows';
    } elseif (preg_match('/Mac/', $commenter_agent)) {
        $platform = 'Mac';
    } elseif (preg_match('/Linux/', $commenter_agent)) {
        $platform = 'Linux';
    } elseif (preg_match('/iPhone/', $commenter_agent)) {
        $platform = 'iPhone';
    } elseif (preg_match('/iPad/', $commenter_agent)) {
        $platform = 'iPad';
    } elseif (preg_match('/Android/', $commenter_agent)) {
        $platform = 'Android';
    } else {
        $platform = 'Unknown';
    }
    
    $comment_text .= '<p>IP地址:' . $commenter_ip . '</p>';
    $comment_text .= '<p>操作平台:' . $platform . '</p>';
    
    return $comment_text;
}
add_filter('comment_text', 'show_commenter_ip_and_platform');

将上述代码添加到主题的functions.php文件中即可。这样,评论部分将会显示评论者的IP地址和操作平台信息。

评论
一个月内的热帖推荐
鸟云
Lv.1普通用户

275

帖子

20

小组

225

积分

赞助商广告
站长交流