我在使用wordpress userprofile编辑时遇到问题。
问题是验证函数是在更新函数之后执行的,所以我在wordpress上得到的是错误但字段已更新。
function bw_admin_validate_fields($errors, $update, $user){
if(isset($_POST["credits"]) && !ctype_digit($_POST['credits'])){
$errors->add("credits_error","The field credits must be a number");
}
if(isset($_POST['ipaddress']) && !filter_var($ip, FILTER_VALIDATE_IP)){
$ip = gethostbyname($_POST['ipaddress']);
if($ip == $_POST['ipaddress'])
$errors->add("ip_error","You must enter a valid ip address or a hostname");
}
}
function bw_admin_save_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) || !ImAdmin() )
return false;
if(isset($_POST['generate']) && isset($_POST["ipaddress"])){
$ip = $_POST['ipaddress'];
if(!filter_var($ip,FILTER_VALIDATE_IP)) $ip = gethostbyname($_POST['ipaddress']);
$key = API::I()->createApi($ip);
update_user_meta($user_id,"api_key",$key);
}
else{
$apikey = trim(get_user_meta($user_id,'api_key',true));
$banned = isset($_POST['banned']) && $_POST['banned'] ? true : false;
if($banned) API::I()->Ban($apikey);
else API::I()->UnBan($apikey);
if(isset($_POST['credits']) && ctype_digit($_POST['credits']))
{
if(((int)$_POST['credits']) < 0) $_POST['credits'] = 0;
API::I()->setCredits($_POST['credits'],$apikey);
}
}
}
当我点击编辑按钮时,wordpress告诉我,我必须输入有效的IP或有效的主机名,但已经创建了Api。