wordpress站点随着网站注册的开放很多相应的问题也随之而来,比如说需要限制注册人群,以及防止机器注册,我们可以添加一个验证问题来解决,虽然添加了验证问答,但是有些蛋疼的人可能知道答案后会死命的注册,使用随机问答可以稍微缓解下这种情况,其实只要我们知道了用户注册时IP只需要对恶意注册的IP进行封锁即可解决这个问题,代码如下:

//透过代理或者cdn获取访客真实IP
function get_client_ip() {
 if (getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_CLIENT_IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") && strcasecmp(getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_CLIENT_IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"), \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_CLIENT_IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
 else if (getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") && strcasecmp(getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"), 
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
 else if (getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") && strcasecmp(getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"), \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
 else if (isset ($_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']) && $_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] 
&& strcasecmp($_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'], \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = $_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'];
 else
  $ip = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
 return ($ip);
}
// 添加一个字段保存IP地址
function log_ip($user_id){
 $ip = get_client_ip();
 update_user_meta($user_id, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', $ip);
}
add_action(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_register\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'log_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');

// 添加“IP地址”这个栏目
function signup_ip($column_headers) {
 $column_headers[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = __(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'IP地址\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');
 return $column_headers;
}
if ( is_multisite() ) {
 // 添加多站点支持
 add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'wpmu_users_columns\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');
} else {
 // 添加单站点支持
 add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'manage_users_columns\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');
}

// 格式化输出内容
function ripms_columns($value, $column_name, $user_id) {
 if ( $column_name == \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ) {
  $ip = get_user_meta($user_id, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', true);
  if ($ip != \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"){
$ret = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'.__($ip, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\').\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
return $ret;
  } else {
$ret = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'.__(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'没有记录\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\').\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
return $ret;
  }
 }
 return $value;
}
add_action(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'manage_users_custom_column\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\',  \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'ripms_columns\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', 10, 3);

以上代码添加到主题的functions.php文件中即可,另外如果想记录用户的登录IP则可以使用以下代码。(PS两段代码不可以同时使用,其中代码二里面已经包含了代码一的功能)

//透过代理或者cdn获取访客真实IP
function get_client_ip() {
 if (getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_CLIENT_IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") && strcasecmp(getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_CLIENT_IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"), \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_CLIENT_IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
 else if (getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") && strcasecmp(getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"), 
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"HTTP_X_FORWARDED_FOR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
 else if (getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") && strcasecmp(getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"), \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = getenv(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
 else if (isset ($_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']) && $_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] 
&& strcasecmp($_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'], \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"))
  $ip = $_SERVER[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'REMOTE_ADDR\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'];
 else
  $ip = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"unknown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
 return ($ip);
}
// 创建一个新字段存储用户注册时的IP地址
add_action(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_register\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'log_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');
function log_ip($user_id){
 $ip = get_client_ip();
 update_user_meta($user_id, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', $ip);
}
// 创建新字段存储用户登录时间和登录IP
add_action( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'wp_login\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'insert_last_login\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' );
function insert_last_login( $login ) {
 global $user_id;
 $user = get_userdatabylogin( $login );
 update_user_meta( $user->ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', current_time( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'mysql\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ) );
 $last_login_ip = get_client_ip();
 update_user_meta( $user->ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', $last_login_ip);
}
// 添加额外的栏目
add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'manage_users_columns\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'add_user_additional_column\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\');
function add_user_additional_column($columns) {
 $columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_nickname\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'昵称\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 $columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_url\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'网站\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 $columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'reg_time\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'注册时间\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 $columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'上次登录\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 // 打算将注册IP和注册时间、登录IP和登录时间合并显示,所以我注销下面两行
 /*$columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'注册IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 $columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'登录IP\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';*/
 unset($columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']);//移除“姓名”这一栏,如果你需要保留,删除这行即可
 return $columns;
}
//显示栏目的内容
add_action(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'manage_users_custom_column\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\',  \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'show_user_additional_column_content\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', 10, 3);
function show_user_additional_column_content($value, $column_name, $user_id) {
 $user = get_userdata( $user_id );
 // 输出“昵称”
 if ( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_nickname\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' == $column_name )
  return $user->nickname;
 // 输出用户的网站
 if ( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_url\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' == $column_name )
  return \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'user_url.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" target=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"_blank\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\">\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'.$user->user_url.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 // 输出注册时间和注册IP
 if(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'reg_time\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' == $column_name ){
  return get_date_from_gmt($user->user_registered) .\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'.get_user_meta( $user->ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'signup_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', true);
 }
 // 输出最近登录时间和登录IP
 if ( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' == $column_name && $user->last_login ){
  return get_user_meta( $user->ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', ture ).\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'.get_user_meta( $user->ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'last_login_ip\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', ture );
 }
 return $value;
}
// 默认按照注册时间排序
add_filter( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"manage_users_sortable_columns\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\", \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'cmhello_users_sortable_columns\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' );
function cmhello_users_sortable_columns($sortable_columns){
 $sortable_columns[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'reg_time\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'reg_time\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
 return $sortable_columns;
}
add_action( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'pre_user_query\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'cmhello_users_search_order\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' );
function cmhello_users_search_order($obj){
 if(!isset($_REQUEST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'orderby\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']) || $_REQUEST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'orderby\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']==\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'reg_time\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ){
  if( !in_array($_REQUEST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'order\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'],array(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'asc\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\',\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'desc\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\')) ){
$_REQUEST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'order\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'] = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'desc\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\';
  }
  $obj->query_orderby = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"ORDER BY user_registered \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\".$_REQUEST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'order\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'].\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
 }
}

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部