Skip to content

Instantly share code, notes, and snippets.

@w3b-beweb
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3b-beweb/9afcbaf70a45773b548e to your computer and use it in GitHub Desktop.
Save w3b-beweb/9afcbaf70a45773b548e to your computer and use it in GitHub Desktop.
modded userpro_side_validate() from ajax.php of wordpress Userpro plugin
function userpro_side_validate(){
global $userpro;
if ( $_POST['action'] != 'userpro_side_validate')
die();
extract($_POST);
$output['error'] = '';
switch($ajaxcheck) {
case 'envato_purchase_code':
if ( !$userpro->verify_purchase($input_value) ) {
$output['error'] = __('Invalid purchase code or Envato API is down.','userpro');
} else {
$output['error'] = '';
}
break;
case 'display_name_exists':
if ($userpro->display_name_exists($input_value)) {
$output['error'] = __('The display name is already in use.','userpro');
}
break;
case 'username_exists':
if (username_exists($input_value)){
$output['error'] = __('Username already taken.','userpro');
} else if ( !preg_match("/^[A-Za-z0-9_]+$/", $input_value) ) {
$output['error'] = __('Illegal characters are not allowed in username.','userpro');
}
break;
case 'email_exists':
if (!is_email($input_value)) {
$output['error'] = __('Please enter a valid email.','userpro');
} else if (email_exists($input_value)) {
$output['error'] = __('Email is taken. Is that you? Try to <a href="#" data-template="login">login</a>','userpro');
}
break;
case 'validatesecretkey':
if (strlen($input_value) != 20) {
$output['error'] = __('The secret key you entered is invalid.','userpro');
} else {
$users = get_users(array(
'meta_key' => 'userpro_secret_key',
'meta_value' => $input_value,
'meta_compare' => '=',
));
if (!$users[0]) {
$output['error'] = __('The secret key is invalid or expired.','userpro');
}
}
break;
}
//if no check was triggered before
if ( $output['error'] == '' ){
$output = apply_filters('custom_ajax_check_callback',$output,$ajaxcheck,$input_value);
}
$output=json_encode($output);
if(is_array($output)){ print_r($output); }else{ echo $output; } die;
}
@w3b-beweb
Copy link
Author

add a custom validation via filter:

// ITALIAN CELL NUMBER
add_filter('custom_ajax_check_callback', 'username_exists_as_cell_phone',10,3);
function username_exists_as_cell_phone($output, $ajaxcheck, $input_value){
    global $userpro;

    if ($ajaxcheck == 'username_exists_as_cell_phone'){
        if ( !preg_match("/^3\d{9}$/", $input_value) ) {
            $output['error'] = __('Is not a valid cell number. Please don\'t use  the international prefix (ex: +39)','userpro');
        } else {
            if (username_exists($input_value)){
                $output['error'] = __('Cell number already used. If it\'s your cell number, try to request a <a href="/cambia-password"><strong>password change</strong></a> to use the account. You will receive a code via SMS to login with this cell number.','userpro');
            }
        }
    }
    return $output;
}
`

@shakensoul
Copy link

This is exactly what I was looking for, only slightly different.

Can you help with the code if I just want to modify the code to do a ajax validation with the phone_number field and prevent registration if phone number already exist?

@w3b-beweb
Copy link
Author

sorry, i'm reading your comment just now! The code above does exactly these 2 things: validate a phone number via regex and, if it is valid, checks if passed phone number is already in use as username. In that case it outputs an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment