It’s been a while since I’ve worked with AJAX and WordPress. The way I used to do things was pretty straight forward and using the function name where the function is created within functions.php then I add action using :
add_action('wp_ajax_nopriv_myfunction', 'myfunction');
add_action('wp_ajax_myfunction', 'myfunction');
I’m drawing a blank here as now I structure my PHP code a little different and my function is no longer located in my functions.php file. It’s located within a class named:
ds_calculators
$.ajax({
type: 'POST',
url: my_ajax.ajaxurl,
data: { action : 'ds_calculators::myfunction', },
success: function(data, textStatus, XMLHttpRequest)
{
//jQuery('#table_listing').html(''); // empty an element
jQuery('.changer').html(data); // put our list of links into it
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
if (typeof console === "undefined")
{
console = {
log: function() {},
debug: function() {},
};
}
if (XMLHttpRequest.status == 404)
{
console.log('Element not found.');
}
else
{
console.log('Error: ' + errorThrown);
}
}
});
I now add action like this
add_action('wp_ajax_nopriv_myFunction', 'ds_calculators::myFunction');
add_action('wp_ajax_myFunction', 'ds_calculators::myFunction');
For the above I’m hitting a 400(Bad Request). How does one include the function name when that function is within a class? Is that my issue or is something else my issue?
3
Answers
You don’t need to specify class reference on JS side:
In php template, if your function is in class, you need to specify it with reference to its instance ($this).
if (myfucntion) method is static, then you’ll use the scope operator with class name ‘ds_calculators::myfunction’
Remember
wp_ajax_nopriv_{$action}
fires non-authenticated Ajax actions for logged-out users.You don’t need to worry too much about namespacing if you are using the wp_ajax hooks.
Is telling wordpress that when a request is sent to the default ajax url with the action set to ‘myfunction’ to pass it to the referenced callback function on resolution. the only thing to watch out for is the the
wp_ajax_${your_function/action_name}
must be the same as the function that it calls on resolution, and that the method in question must be publicHere Is a great writeup on how to implement the wordpress ajax handler in a class, and where I stole this from
If you are interested in using the REST API alongside/instead of the ajax api here is a quick class I made to go though how that would be done and showing namespacing in that setting, along with covering the basics of the wp-nonce
You should fix your ajax call and move action argument next to the others like type, url, success instead of inside data. You are just declaring te action name and it’s not related to php class hierarchy. The actual action name it’s connected to wp_ajax_{action} and wp_ajax_nopriv_{action}. That’s the reason the action name myFunction have to be used as add_action(‘wp_ajax_myFunction‘, ‘ajax_callback’);. More about that you can read here: https://developer.wordpress.org/reference/hooks/wp_ajax_action/
Moving to PHP side…
You are able to call the methods inside of the class object in two ways: