max-kk
3/26/2019 - 6:32 PM

lrm_verify_username_via_api

lrm_verify_username_via_api

<?php
// COpy after this line
add_filter('lrm/users_can_register', function($bool) {
	$user_login = sanitize_user(trim($_POST['username']));
	
	if (!$user_login) {
		return $bool;
	}
	
	/** @var array|WP_Error $response */
	$response = wp_remote_get( 'https://api.condacore.com/users/?name='.$user_login );
	 
	if ( is_wp_error( $response ) ) {
		wp_send_json_error(array('message' => 'Error during Username validation!', 'source'=>'is_wp_error'));
	}
	 
	if ( is_array( $response ) ) {		
		$body    = $response['body']; // use the content
		$res = json_encode($body);
		if ( json_last_error() === JSON_ERROR_NONE ) {
			if ( ! $res->available ) {
				wp_send_json_error(array('message' => 'Sorry, but this Username is already taken!'));
			}
		} else {
			wp_send_json_error(array('message' => 'Error during Username validation!'));
		}
	}
	return $bool;
});