I'm familiar with using ajax in the ordinary way with jQuery.
I've played around it for a while, but don't understand what Wordpress needs to get it to work...
What I have here is taken from some tutorial or article.
This is in functions.php (in a child theme):
// code to load jquery - working fine
// code to load javascript file - working fine
// ENABLE AJAX :
function add_ajax()
{
wp_localize_script(
'function',
'ajax_script',
array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
$dirName = get_stylesheet_directory(); // use this to get child theme dir
require_once ($dirName."/ajax.php");
add_action("wp_ajax_nopriv_function1", "function1"); // function in ajax.php
add_action('template_redirect', 'add_ajax');
The jQuery itself is loading and working fine.
I have tried some basic ajax like the following:
jQuery(document).ready(function($){
$('a.link').click(function(){
$.ajax({
url: ajax_script.ajaxurl,
data: ({action : 'function1'}),
success: function(data){
$('#result').html(data);
}
});
return false;
});
});
Besides this, I don't know how I can test to see if it's even loaded correctly to begin with...
Any help here would be appreciated.
EDIT:
In firebug this error:
ReferenceError: ajax_script is not defined
url: ajax_script.ajaxurl,
See Question&Answers more detail:os