I’m using the following function in functions.php
to check if a username exists in the DB
function check_username() {
$username = $_POST['user'];
if ( username_exists( $username ) ) {
$return['user_exists'] = true;
}
else {
$return['user_exists'] = false;
}
echo json_encode($return);
die();
}
add_action('wp_ajax_check_username', 'check_username');
The problem is that the username_exists()
function only seems to work if a user is already logged in and checks a username. I tried to check for an existing username without logging in and it just returns undefined
for me.
I have no idea why this is happening and can’t find any documentation about this issue.
My question is, how do I enable a user to check if a user exists without having to log in?
Thanks
try this
wp_ajax_check_username – anonymous / non-logged in visitors
wp_ajax_nopriv_check_username– logged in users