Monday, May 27, 2024

WordPress Ajax working example

Wordpress trick to check term id has parent term or child term

Hello to all, welcome to In this post, I will tell you, WordPress Ajax working example.

If you are new in WordPress then please check old post related to WordPress.

In this post, I am showing very simple WordPress Ajax working example but sometime this takes lot of time to find the solution.

Here is I am showing the working code snippet and please follow carefully:

1. Here is the code and you can add this code into any of your theme’s template file:

<!-- Button Click for fire ajax request -->
<button type="submit" class="btn btn-default" id="addProductUrl">Submit</button>

<!-- Ajax Request Code -->
  jQuery(document).ready(function($) {
    $("#addProductUrl").click(function(event) {
      /* Act on the event */
      var formData = new FormData();
      formData.append('action', 'foobar');
      formData.append('id', '12345');
          url: '<?php echo admin_url('admin-ajax.php'); ?>',
         type: 'post',
         contentType: false,
         processData: false,
         data: formData,
         success: function (response) {
             //Get Ajax request response
         error: function (response) {



2. Here is the working code and you need to add into theme’s functions.php file:

add_action( 'wp_ajax_foobar', 'my_ajax_foobar_handler' );
add_action('wp_ajax_nopriv_foobar', 'my_ajax_foobar_handler'); 
function my_ajax_foobar_handler() {
    echo $_POST['id'];




This is it. If you have any query related to this post, then please do comment below or ask question.


Thank you

the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.