I am seeking the best way to diagnose what is causing my Ajax calls to /wp_admin/admin-ajax.php to be re-directed to my site's homepage. I'm writing a new custom plugin for our site, following the pattern of another (working) plugin that uses admin-ajax. The existing plugin is working fine, but my new plugin's calls to admin-ajax always get re-directed home. I have already corrected what seemed the most likely culprit. We have a login-re-direct plugin that sends users to the right landing page based on their role, so I updated that to include a test for DOING_AJAX before running the re-direct logic, but that did not help: if (is_admin() || !$post || DOING_AJAX) { return; } I can see in the browser that my Ajax call is happening, and I see the 302 re-direct to our home page. Here's my PHP and JS code for completeness' sake: function updateAvailability(e){ var active_select = document.getElementById("availability_" + e); var newAvailability = active_select.value; var respJSON = ""; console.log('display_user_company_products updating availability for product: ' + e + ' to : ' + newAvailability); jQuery.ajax({ type : "post", dataType : "json", url : "https://xxxxxx/wp_admin/admin-ajax.php", data : {'action': 'update_product_master', 'product_id': e, 'attribute': 'current_availability', 'value': newAvailability, }, success: function(response) { respJSON = JSON.stringify(response); console.log('display_user_company_products AJAX call returned : ' + respJSON); if(response.type == "success") { alert("Thank you for updating your info!"); } else { respJSON = JSON.stringify(response); alert("Your update could not be processed"); } } }); } // Create admin-ajax function to update product master attributes based on call from B2B Portal add_action("wp_ajax_update_product_master", "ajax_update_product_master",1); add_action("wp_ajax_nopriv_update_product_master", "ajax_update_product_master",1); function ajax_update_product_master() { if (!$product_id) { $product_id = isset($_POST['product_id'])?$_POST['product_id']:isset($_GET['product_id'])?$_GET['product_id']:0; } if (!$attribute) { $attribute = isset($_POST['attribute'])?$_POST['attribute']:isset($_GET['attribute'])?$_GET['attribute']:0; } if (!$value) { $value = isset($_POST['value'])?$_POST['value']:isset($_GET['value'])?$_GET['value']:0; } error_log("Function ajax_update_product_master updating product master : ".$product_id." attribute : ".$attribute." with value : ".$value); $result = array('success'=> 1, 'message'=>'Function ajax_update_product_master updating product master '.$cigar_id.' attribute '.$attribute.' with value '.$value); return json_encode($result); } I'm completely stumped why admin-ajax never runs my php function above, but instead re-directs my call to our homepage. What kind of methodical process can I use to diagnose what is causing these re-directs? I tried the code included above, and was expecting my PHP function ajax_update_product_master to be executed by WP admin-ajax, but instead received a 302 re-direct to homepage.
Jese Leos
August 10, 2024
Verified user
A friend helped me tweak a few things and this is now working, using JQuery and WP admin-ajax. My biggest problem was the failure to localize my script correctly.