So I'm trying to set up an AJAX action for a plugin I'm building.
When using /wp-admin/admin-ajax.php?action=beacon_podio-get_apps
I was just getting hello world0
and I'm not seeing Request is valid
or the Invalid request
so it seems like the action is not being called
I think I'm missing something but I'm not sure what I'm missing.
class testClass {
public function __construct(){
echo "hello world";
add_action('wp_ajax_beacon_podio-get_apps', array($this, "get_apps"));
}
public function get_apps(){
if(isset($_POST['app_id'])){
$app_id = $_POST['app_id'];
die("Request is valid");
}else{
die("Invalid request");
}
}
}
new testClass();
I have been reading but it's missing the URL I'm supposed to be using.
So I'm trying to set up an AJAX action for a plugin I'm building.
When using /wp-admin/admin-ajax.php?action=beacon_podio-get_apps
I was just getting hello world0
and I'm not seeing Request is valid
or the Invalid request
so it seems like the action is not being called
I think I'm missing something but I'm not sure what I'm missing.
class testClass {
public function __construct(){
echo "hello world";
add_action('wp_ajax_beacon_podio-get_apps', array($this, "get_apps"));
}
public function get_apps(){
if(isset($_POST['app_id'])){
$app_id = $_POST['app_id'];
die("Request is valid");
}else{
die("Invalid request");
}
}
}
new testClass();
I have been reading https://codex.wordpress/AJAX_in_Plugins but it's missing the URL I'm supposed to be using.
Share Improve this question edited Mar 28, 2019 at 21:35 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Mar 28, 2019 at 20:03 Martin BarkerMartin Barker 1014 bronze badges 1- @Krzysiek Dróżdż why did you remove the plugin-developer tag this is clearly a plugin development issue? – Martin Barker Commented Mar 29, 2019 at 15:20
2 Answers
Reset to default 2OK, so you misunderstood it a little bit, I guess...
You do the first part correctly. So yes - AJAX requests should be sent to wp-admin/admin-ajax.php
and the should have action
param set in the request (either as a POST or as a GET).
But then, you do it wrong. You register your action with this code:
add_action('beacon_podio-get_apps', array($this, "get_apps"));
But id should be:
add_action('wp_ajax_beacon_podio-get_apps', array($this, "get_apps"));
or (for anonymous users)
add_action('wp_ajax_nopriv_beacon_podio-get_apps', array($this, "get_apps"));
So just to make it clear - the correct hooks are:
wp_ajax_(action)
wp_ajax_nopriv_(action)
where (action) is the action that you sent as action parameter.
This appeared to be a bug in WordPress, coming back to this and upgrading to version 5.2 the problem seems to have been fixed for some reason the has_action was failing to find the action unsure why but it seems to have been fixed.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477772a4629425.html
评论列表(0条)