I try to make an ajax function in wordpress and I enqueued my script and declared my variables like this in functions.php
:
function my_enqueue_scripts() {
wp_enqueue_script(
'load_post',
get_template_directory_uri() . '/js/load_post.js',
array( 'jquery' ),
'1.0',
1
);
wp_localize_script(
'load_post',
'wp_ajax',
array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax_nonce' )
)
);
}
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');
load_post.js :
jQuery(document).ready(function($) {
jQuery('#next-container').click(function($) {
var $container = jQuery('#isotope-container');
var IDs = [];
jQuery(".element").each(function($){ IDs.push(this.id); });
jQuery('#next-container').html('loading');
jQuery.ajax({
type: 'POST',
url: wp_ajax.url,
dataType: 'html',
data: { action: 'load_post',
ajax_nonce: wp_ajax.nonce,
post_ids: IDs,
}, //{'post_ids[]': IDs },
success: function(data) {
jQuery('#next-container').html('');
$container.isotope( 'insert', $(data));
}
})
})
})
However in my load_post.js
I can't get wp_ajax
variable. I don't see any variable added in the head section of my source code in google chrome or IE.
Where comes from my problem? Do I miss something?