Try die() function in the end of PHP function. It will helps.
UPD: Ive made some simple version of your problem code to check it and it works. I think it will helps you.
t returns 0.
You are able to change inner PHP script to your logic and it won
PHP:
function more_post_ajax(){
echo "Hello";
die();
}
add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
function custom_scripts_init(){
wp_enqueue_script( 'custom', get_template_directory_uri().'/assets/js/custom.js', array('jquery') );
wp_localize_script( 'custom', 'ajaxPosts', array(
'customUrl' => admin_url( 'admin-ajax.php' ),
'noposts' => 'No Older Posts Found',
));
}
add_action('wp_footer', 'custom_scripts_init');
JQuery:
jQuery(document).ready(function(){
jQuery("#page").append("<button type="button" class="load-more"/>");
var ppp = 2; // Post per page
var pageNumber = 1;
function load_posts(){
pageNumber++;
var str="&pageNumber=" + pageNumber + '&ppp=' + ppp + '&action=more_post_ajax';
jQuery.ajax({
type: "POST",
dataType: "html",
url: ajaxPosts.customUrl,
data: str,
success: function(data){
console.log(data);
},
error : function(jqXHR, textStatus, errorThrown) {
//$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
return false;
}
jQuery(".load-more").on("click",function(e){ // When btn is pressed.
// jQuery(".load-more").attr("disabled",true); // Disable the button, temp.
load_posts();
e.preventDefault();
});
});