How to use AJAX with WordPress

Here is the simple tutorial to do so.

On Client Side (JAVASCRIPT)

$(document).on('click', '.remove', function(){
let url = "<?=admin_url('admin-ajax.php')?>";
action : "ajax_test", 
id : "1",
alert("Data: " + data + "\nStatus: " + status);

On Server Side: (In Plugin or function.php file)

The wp_ajax_{action} hook only fires for logged in users. For logged-out users the action wp_ajax_nopriv_{action} is triggered on an ajax request – so you need to hook into that as well.


add_action("wp_ajax_ajax_test",  "ajaxTest");
add_action("wp_ajax_nopriv_ajax_test",  "ajaxTest"); function ajaxTest(){ echo "I am a simple ajax"; } ?>