So I am trying to make a form that validates if an url exists dynamically and am having problems with Wordpress ajax call ALWAYS returning 0. I have also been researching for about 3 hours now about this and nothing seems to work for me, it might be a dumb question. Here is what I have done so far:
I have a button:
<input id="analyze"type="button" class="analyze-button" value="Analyze">
And I am trying to run this script on it (located in another file):
$( document ).on( 'click', '#analyze', function() {
$.ajax({
url: checkurl.ajax_url,
type:'POST',
data:{
action: 'checkUrl',
//link_check : $('#main-input').val(),
},
success: function(msg){
alert(msg);
},
error:function(msg){
alert("Error");
}
});})
This is how I added the ajax hooks:
add_action( 'wp_ajax_nopriv_checkUrl', 'checkUrl' );
add_action( 'wp_ajax_post_checkUrl', 'checkUrl' );
function checkUrl() {
echo "I works1";
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
//$url = $_POST['main-input'];
//echo $url;
echo "I work";
}
echo "I works";
die();
}
And this is how I enqueued the script:
function ajax_contact() {
wp_enqueue_script( 'ajaxcontact', get_template_directory_uri() .'/templates/ajaxcontact.js', array('jquery'), '1.0', true );
wp_localize_script( 'ajaxcontact', 'checkurl', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}
add_action( 'wp_enqueue_scripts', 'ajax_contact' );
Last 2 code snippets were from function.php
What am I doing wrong? I tried a lot of things, but with no luck so far.
This is the link to the page where I have the form. It is a test page right now. Maybe this helps.