0

I'm writing a plugin that calls the output of another plugin's ajax, which is set-up at wp_ajax_

I would have called the functions directly, but limitations in the plugin make that a non-option.

To make it clear, this is what's happening:

  • User selects an action, then hits apply
  • My plugin function is called in admin-ajax.php (via the wp_ajax_ hook)
  • The plugin function creates a nonced url like this: $url = wp_nonce_url( admin_url( 'admin-ajax.php?action=plugin_action&post_id=' . $post_id), 'plugin_action' );
  • then I call that url with wp_remote_get

output is 0.

Now the strange thing: instead of calling the url with wp_remote_get I echoed it, then copy pasted it in my browser and it gave me the correct output. (so it's not a matter of a missing die()!)

I've also tried putting my own function in edit.php (using the load- hook) instead of admin-ajax.php too, but the result was the same (0). Now the codex shows that the load- hook is executed before wp_ajax, so that's expected behaviour (the action is not added yet) but even in wp_ajax with a priority of 99 I get 0.

Any ideas on what's going on? Could it be that the action is not added yet when I call it directly from admin-ajax.php? If so, is there a better place to put my own action? (one that fires after wp_ajax)

1 Answer 1

1

Does your server know its own name and/or have loopback connections allowed?

Like, if you do a remote get on just the main blog page, does that work?

If the server doesn't know its own name because the DNS doesn't resolve, or loopback connections are disabled at the server level (these are common "security" measures on some cheap hosting), then connecting back to yourself won't work.

Edit: Okay, so if that is not it, then did you try hooking to the wp_ajax_nopriv_ hook? Since you're not passing cookies along or any form of credentials with wp_remote_get, then you will be connecting as an unprivileged user, which means it'll use the nopriv hook instead of the normal priv'd hook.

4
  • I tried a wp_remote_get(site_url()) which worked perfectly, so I guess it's something else. Moreover, the 0 suggests that admin-ajax is fired correctly, it's just not recognizing/or properly firing the action. (otherwise I would have expected an empty screen or error)
    – Ewout
    Apr 16, 2013 at 19:01
  • Added a bit about using the nopriv hook instead. Try that.
    – Otto
    Apr 18, 2013 at 16:01
  • Thank you for being so helpful! I never realised that I don't have anything that will inform wp_ajax that I'm allowed to access it and you're probably right that it then uses wp_ajax_nopriv_. The thing is, I cannot change the hook of the other plugin, since that already lives in wp_ajax. Now I have found another way around this that works directly with the functions of that other plugin. I'm still interested to hear any suggestions on how to approach this though!
    – Ewout
    Apr 19, 2013 at 12:52
  • You can add your own action hook connections. You don't need to edit the other code to do that.
    – Otto
    Apr 22, 2013 at 16:29

Your Answer

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

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