admin管理员组文章数量:1429620
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.
本文标签: WordPress ajaxaction how to return content
版权声明:本文标题:WordPress ajax-action how to return content 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745477772a2660041.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论