XanderYe的个人小站

wordpress优化总结

首先声明版本:wordpress: 5.3.2,nginx: 1.21.3

评论ip

我使用的是docker部署的wordpress,然后再用nginx反向代理,所以导致评论时后台获取不到真实ip。修改nginx的配置,添加 proxy_set_header X-Forwarded-For $remote_addr; 就可以了。

然后打算做个评论根据ip显示地理位置的功能,首先是找免费接口,百度一下多的是,我这里用的百度地图,需要申请AK,接下来修改代码:

打开主题编辑器,找到function.php,添加函数

function get_locate($ip) {
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt ($ch, CURLOPT_URL, 'https://api.map.baidu.com/location/ip?ak=你的AK&ip='.$ip.'&coor=bd09ll');  
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
    $file_contents = curl_exec($ch);  
    curl_close($ch);  
    $result = json_decode($file_contents,true);
    if ($result['status'] == 0) {
        return $result['content']['address'];
    } else {
        return "未知";
	}
}

然后找comments.php,找到 wp_list_comments 函数,不同主题回调函数不一样,我使用的是MDx主题,回调函数是 mdx_comment_format

https://wp.xanderye.cn/wp-content/uploads/2020/03/QQ图片20200308164403.png

全局搜下 mdx_comment_format , 在function.php中,修改样式,添加

<?php echo get_locate(get_comment_author_IP());?>

头像加载不出

我这里直接改wp源代码中的gravatar地址为镜像地址,找到wp-includes/link-template.php,搜索secure.gravatar.com,大概4228行左右,整个if注释掉,改成 $url = 'https://gravatar.loli.net/avatar/' . $email_hash;

https://wp.xanderye.cn/wp-content/uploads/2021/10/image-5.png

高亮插件

我用的 Enlighter

评论通知

发邮件我用的 WP Mail SMTP 插件,评论通知用的 Comment Notifier 插件

赞赏

发表评论

textsms
account_circle
email

XanderYe的个人小站

wordpress优化总结
首先声明版本:wordpress: 5.3.2,nginx: 1.21.3 评论ip 我使用的是docker部署的wordpress,然后再用nginx反向代理,所以导致评论时后台获取不到真实ip。修改nginx的配置,添加 pro…
扫描二维码继续阅读
2020-03-08