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 https://codex.wordpress.org/AJAX_in_Plugins but it's missing the URL I'm supposed to be using.

Output from wordpress

1
  • @Krzysiek Dróżdż why did you remove the plugin-developer tag this is clearly a plugin development issue? Commented Mar 29, 2019 at 15:20

2 Answers 2

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.

6
  • 2
    Also, IIRC, the wp_ajax_nopriv_* actions don't run if the user is signed in, so if you want it to run whether the user is logged in or not, you need to hook to both.
    – Pat J
    Commented Mar 28, 2019 at 20:38
  • ok so i have changed it to add_action('wp_ajax_beacon_podio-get_apps', array($this, "get_apps")); and i'm getting the same thing im still not seeing the output with trying ?action=beacon_podio-get_app and ?action=wp_ajax_beacon_podio-get_app Commented Mar 28, 2019 at 20:59
  • I have updated question to show the new code and that I'm still getting the same error. Commented Mar 28, 2019 at 21:03
  • Thanks for clarifying the URL, but it does mean one of the URL's I was using was correct I am logged in and want it to be for logged in only users. so I'm using the correct one. but it still is not working Commented Mar 28, 2019 at 21:10
  • No, it only means that you're ignoring a large part of answer (and original article from Codex)... And sorry, I can't help with that. I've already mentioned it twice. Commented Mar 28, 2019 at 21:20
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.