You’re POSTing user_name
but in your code you’re checking for register_name
, you should instead check $_POST['user_name']
instead.
You should also be checking to make sure a value is actually being passed as well, this is how I would do it instead (setting unavailable as default):
function mpl_check_username() {
$response = array(
'status' => 'unavailable',
'text' => __( 'Username unavailable' )
);
$username = array_key_exists( 'user_name', $_POST ) ? sanitize_text_field( $_POST['user_name'] ) : false;
if ( $username && ! username_exists( $username ) ) {
$response['status'] = 'available';
$response['text'] = __( 'Username available' );
}
echo json_encode( $response );
die();
}
All the username_exists
function does is this code below:
$user = get_user_by( 'login', $username );
if ( $user ) {
$user_id = $user->ID;
} else {
$user_id = false;
}
You could also try this in your code to remove the possibility of the username_exists
filter being used to return true for some weird reason.