Stop ajax listener from sending empty parameter to function

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’; } … Read more

WordPress search results with Ajax, get_post_type() not working

$_GET[‘s’] will not work for two reason. Firstly you make a POST request and not a GET request and secondly there is no variable with name s in your request. When executing an AJAX request through wp-admin/admin-ajax.php WordPress executes only the code (function) registered to that request. Also it loads WordPress Administration APIs and Ajax … Read more

contact form ajax empty response error message

jQuery AJAX will trigger an error event not only when it receives an HTTP status code indicating a problem with the request, but also if jQuery fails to parse the response body. Since you’re using die() without sending any response body, it’s likely that jQuery is choking on the “empty” response. Using wp_send_json_success() instead of … Read more

How do I query posts by a sub value with the API?

To access posts via meta key you need to hook into the [rest_query_vary filter].1 The example I was thinking of when I read your question is the same one found on the post @WebElaine commented. I’ve merely added it below: function my_allow_meta_query( $valid_vars ) { $valid_vars = array_merge( $valid_vars, array( ‘meta_key’, ‘meta_value’ ) ); return … Read more

Customizer AJAX using buttons

Having any AJAX inside of the customizer defeats the reason for using the customizer in the first place. The idea of the customizer is that it fully controls the state of the settings while it is active, and having any setting being updated without going via the customizer logic can actually cause a mismatch between … Read more