最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WordPress ajax-action how to return content

programmeradmin1浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 2

OK, 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.

发布评论

评论列表(0)

  1. 暂无评论