Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active November 5, 2022 21:54
Show Gist options
  • Save patrickfreitasdev/f459084e49bb81dacf614602f0514fbe to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/f459084e49bb81dacf614602f0514fbe to your computer and use it in GitHub Desktop.
<?php
add_action('wp_footer','custom_js_form_validation');
function custom_js_form_validation(){
?>
<script>
jQuery( document ).ready(function($) {
$(".store-name-forminator input").on('blur', function (e) {
var _field = $(this);
var _field_val = _field.val();
$.ajax({
url : '<?php echo admin_url( 'admin-ajax.php' ); ?>',
type: "POST",
data: {'action': 'check_store_availability_forminator', store: _field_val},
dataType: "json",
success: function(response) {
console.log(response.status);
if(response.status == 'unavailable'){
$('.custom-validation-store').remove();
$('.store-name-forminator input').parent().parent().append('<label class="forminator-label--validation custom-validation-store">Store is not available.</label>');
}else{
$('.custom-validation-store').remove();
$('.store-name-forminator input').parent().parent().append('<label class="forminator-label--validation custom-validation-store">Store is available.</label>');
}
}
});
});
});
</script>
<?php
}
add_action('wp_ajax_nopriv_check_store_availability_forminator', 'check_store_availability_forminator');
add_action('wp_ajax_check_store_availability_forminator', 'check_store_availability_forminator');
function check_store_availability_forminator() {
$response = array();
$store = sanitize_text_field( $_POST['store'] );
$stores = get_user_by('slug', $store);
if( $stores ){
$response['status'] = 'unavailable';
$response['text'] = __('Custom text that can be used later');
}else{
$response['status'] = 'available';
$response['text'] = __('Custom text that can be used later');
}
echo json_encode($response);
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment