I ended up just abstracting the actual validation to another function:
function ajax_checkEmailAddress($passedEmail = false) {
$sentEmail = sanitize_email($_POST['email'], true);
$jsonArray = validateEmailAddress($passedEmail);
wp_send_json($jsonArray);
}
add_action('wp_ajax_checkEmailAddress', 'ajax_checkEmailAddress');
add_action('wp_ajax_nopriv_checkEmailAddress', 'ajax_checkEmailAddress');
function validateEmailAddress($email) {
if (email_exists($email)) {
$jsonArray['response'] = 'error';
$jsonArray['message'] = __('This email address is already in use.', 'ajaxSignup');
} else {
$jsonArray['response'] = 'success';
}
return $jsonArray;
}
This way, validateEmailAddress
can be called from another PHP function without issue, and the ajax function doesn’t have to try to decipher if it’s actually a direct ajax call or not.
I still have no idea as to why wp_ajax
sends an empty parameter, but I would be intrigued to know.