AJAX and -1 response

You should end all “async” functions with die() or exit, otherwise WordPress does it for you returning -1.

Pagination Using ajax

I had got the ans. First you have to add following code in your function.php to call ajax in your template** add_action( ‘wp_ajax_demo-pagination-load-posts’, ‘cvf_demo_pagination_load_posts’ ); add_action( ‘wp_ajax_nopriv_demo-pagination-load-posts’, ‘cvf_demo_pagination_load_posts’ ); function cvf_demo_pagination_load_posts() { global $wpdb; // Set default variables $msg = ”; if(isset($_POST[‘page’])){ // Sanitize the received page $page = sanitize_text_field($_POST[‘page’]); $cur_page = $page; $page -= … Read more

Ajax Request for both logged and non logged users

You can add the function to both hooks: add_action(‘wp_ajax_ajaxTest’, ‘ajaxTest’); add_action(‘wp_ajax_nopriv_ajaxTest’, ‘ajaxTest’); But, there’s a better way to do it, use a REST API endpoint: add_action( ‘rest_api_init’, function () { register_rest_route( ‘yourstuff/v1’, ‘/test/’, array( ‘methods’ => ‘POST’, ‘callback’ => ‘yourstuff_test_endpoint’ ) ); } ); function yourstuff_test_endpoint( $request ) { $stuff = $request[‘stuff’]; return “Received “.esc_html( … Read more

Bad request 400 from custom ajax call

There’s only one reason you get a 400 error with admin-ajax.php: The wp_ajax_{$action} or wp_ajax_nopriv_{$action} action has not been hooked with an {$action} that matches the action data parameter sent with your request. So either your callback is not hooked, or you’re not sending the action correctly. Your code is making both errors. You are … Read more

Exposing data using a custom WordPress API

You need a die(); before the closing } of the function and you might want to add the relevant php header to tell the browser it’s content-type I wouldn’t worry about the browser returning the wrong content type. As long as your code loading the data treats it as JSON So for your function try … Read more

AJAX Call is Only Returning 100 Results from Query

As otto implied in his comment, your code execution time is probably greater then the max php execution set in max_execution_time configuration directive in your php.ini, which is usually 30 seconds. You might be able to change that directive in php.ini or .htaccess, but it will only create a new problem as the user will … Read more