8

WordPress如何禁用评论存储IP地址

 2 years ago
source link: https://www.huhexian.com/25985.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WordPress如何禁用评论存储IP地址

2022-02-1013:00:09评论665字

默认 WordPress 会在后台存储评论者的 IP 地址,主要是用于反垃圾评论,比如 Akismet 之类的插件,会通过 IP 判断垃圾评论。但 IP 属于个人隐私,不存储评论者 IP,可能会让用户对你的网站更有信任感。

WordPress如何禁用评论存储IP地址

我们可以通过下面的代码移除这个功能,将代码添加到当前主题函数模板 functions.php 中:

  1. add_filter( 'pre_comment_user_ip', 'zm_remove_comments_ip' );
  2. function zm_remove_comments_ip( $comment_author_ip ) {
  3. return '';

之后,评论者的 IP 地址不会再存储。

也可以安装 Remove IP 插件实现同样的功能,插件中也只有一段类似的代码,将所有 IP 替换为 127.0.0.1 而已。

  1. add_filter('pre_comment_user_ip', 'pre_comment_anon_ip');
  2. function pre_comment_anon_ip() {
  3. $REMOTE_ADDR = "127.0.0.1";
  4. return $REMOTE_ADDR;

虽然通过添加上面的代码不再存储 IP 地址,但之前评论 IP 地址已存储在数据库中,如果想删除,可以将下面代码同样添加到当前主题函数模板 functions.php 中:

  1. global $wpdb;
  2. $wpdb->query( "UPDATE wp_comments SET comment_author_IP=''" );

然后刷新页面,之前存储的评论者 IP 地址将从数据库中删除,该代码不需要保留在主题中,用后请移除。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK