I have looked over many posts on here and other websites and nothing I do seems to work.
What I am doing is, I have a custom template file and inside that file I have a div that I want to populate using jquery ajax. The problem is that the response is always 0.
Note: I have used this code before in a plugin and not a template file and it worked the way it was supposed to. This makes me wonder if the add_action code isn't working right.
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery.ajax({
type : "post",
dataType : "html",
url : "<?php echo site_url(); ?>/wp-admin/admin-ajax.php",
data : {action : "tw_pages_results", 'page':0, 'itemsperpage':<?php echo $item_per_page; ?>, nonce: "<?php echo $tw_pages_nonce; ?>"},
success: function(response) {
jQuery("#results").html(response);
jQuery("#1-page").addClass('active');
}
});
});
</script>
This is just the beginning part of the function in the file loaded by the ajax.
add_action("wp_ajax_tw_pages_results", "tw_pages_results");
add_action("wp_ajax_nopriv_tw_pages_results", "tw_pages_results");
function tw_pages_results() {
if ( !wp_verify_nonce( $_POST['nonce'], "tw_pages_nonce")) {
exit("No naughty business please");
}