WordPress development<\/a> but to my own stupidity and tiredness while coding, was that I actually forgot to include my ajax.php file into my functions.php file. This meant that the server side function associated with my AJAX call actually never existed. It took 2 hours of debugging until I realised – what a nightmare!<\/p>\nThe reason I’m adding this warning into this article, is because it actually forced me to learn a subtle nuance of WordPress’ AJAx implementation. The function essentially didn’t exist, but WordPress was returning ‘0’ to the AJAX call response. It didn’t return anything like ‘function doesn’t exist’ etc. – it simply returned an integer of 0. This is the reason it took me so long to debug – it wasn’t immediately obvious that my function wasn’t even included as it would be if this wasn’t an AJAX call.<\/p>\n
The server side code<\/h2>\n There are a few things to watch out for in your PHP function that takes the AJAX call.<\/p>\n
Logged in or logged out<\/h3>\n As a security precaution, WordPress requires you to explicitly make\u00a0the AJAX function accessible to non-authenticated users. To enable the AJAX call to non-auth users, you must include the following action:<\/p>\n
\r\n\r\nadd_action( 'wp_ajax_nopriv_review_vote_ajax_request', 'review_vote_ajax_request' );\r\n\r\n<\/pre>\nDon’t forget to\u00a0replace\u00a0‘_review_vote_ajax_request’ with your own function name.<\/p>\n
You also need to include the action to explicitly enable this for authenticated users:<\/p>\n
\r\n\r\nadd_action( 'wp_ajax_review_vote_ajax_request', 'review_vote_ajax_request' );\r\n\r\n<\/pre>\nKill the function<\/h3>\n You must include the following at the end of your PHP AJAX function:<\/p>\n
\r\n\r\nwp_die();\r\n\r\n<\/pre>\nEcho not return<\/h3>\n I’m used to returning my AJAX responses from the server in Laravael where I’ll ‘return’ the response. However you must echo out your response back to AJAX, instead of returning it.<\/p>\n
The final code<\/h2>\n Here’s my javascript:<\/p>\n
\r\n\r\njQuery(function($) {\r\n\r\n$('body').on('click', '.review-helpful-answer', function(e) {\r\ne.preventDefault();\r\n\r\nvar $reviewId = $(this).attr('data-review-id');\r\n\r\nvar post_id = $(this).data('id');\r\n$.ajax({\r\ntype:&quot;POST&quot;,\r\nurl: ajaxurl,\r\ndata: {\r\naction: &quot;review_vote_ajax_request&quot;,\r\npost_id: post_id\r\n},\r\nsuccess:function(data){\r\nalert(data);\r\n},\r\nerror: function(errorThrown){\r\nalert(errorThrown);\r\n}\r\n});\r\n\r\n})\r\n\r\n});\r\n\r\n<\/pre>\nAnd the PHP function:<\/p>\n
\r\n\r\nfunction review_vote_ajax_request() {\r\n\r\n\/\/ do what you want with the variables passed through here\r\n\r\n$post_id = $_REQUEST['post_id'];\r\n\r\necho $post_id;\r\n\r\nwp_die();\r\n}\r\n\r\nadd_action( 'wp_ajax_nopriv_review_vote_ajax_request', 'review_vote_ajax_request' );\r\nadd_action( 'wp_ajax_review_vote_ajax_request', 'review_vote_ajax_request' );\r\n\r\n<\/pre>\n <\/p>\n","protected":false},"excerpt":{"rendered":"
Edit: the output of this tutorial can be seen with the ‘agree’ and ‘disagree’ buttons here:\u00a0http:\/\/www.youthenme.com\/sites\/review\/ I’m fairly experienced with using AJAX and\u00a0its various jQuery shorthand implementations, but having never implemented AJAX into a WordPress theme I’ve had to do quite a bit of Googling. I’m writing this post to show how I’ve implemented AJAX…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[28],"yoast_head":"\n
Wordpress AJAX tutorial - how to use AJAX in a Wordpress theme or template - JoeTannorella.com<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n