Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Update

I got it to fire using the following curl command: curl -H "Accept: application/json" -X POST http://localhost/wordpress/wp-admin/admin-ajax.php -d "action=vixo_wordpress_signon&data=banjometer" --trace -

Not really sure how to work out to get it to accept json, but hey...

Original Question

I am new to WordPress and I am having problems getting Ajax to work - but I am also having problems understanding how WordPress loads PHP.

I have a plugin file:

/usr/share/wordpress/wp-content/plugins/myplugin.php

It has the following code in it:

// set up the ajax calls
add_action('wp_ajax_my_ajax', 'my_ajax');
add_action('wp_ajax_nopriv_my_ajax', 'my_ajax');  

function my_ajax () {
    debug_log ("My ajax fired...");
    echo "banjolalia...";
    die ();
}

I want to demonstrate that the ajax has fired using Curl:

curl -X POST http://localhost/wordpress/wp-admin/admin-ajax.php -d '{"action":"my_ajax","data":"banjometer"}' --trace -

(The home page is at http://localhost/wordpress)

I am expecting to see curl return banjolalia and a line appear in the debug log.

What I see is the function returns 0

The Ajax is standalone because I am writing single-signon code and I need the child service to query WordPress and see if the user is logged in.

Because I am new to WordPress/PHP I do not have a robust development cycle.

The way I have been (trying) to get the code to reload is by Deactivating/Activating the plugin on the Admin page. This doesn't seem to work. I wasn't sure if my changes were loading into WordPress so I actually switched the lappie off and on again - but still the Ajax didnt' fire.

I have 2 debugging Plugins installed:

  • Debug-Bar
  • Debug-Console-Bar

The problems I have from a Dev perspective are:

  • how can I use the PHP console to check what code is loaded/force a reload?
  • how can I use the PHP console to see what actions are registed on what hooks?
share|improve this question
    
If I go into admin-ajax.php and run debug comments in there it turns out that it doens't get the action through. –  Gordon Guthrie Jan 21 '13 at 20:11
add comment

2 Answers

up vote 1 down vote accepted

You have nothing specific to do to "load" modified php code in Wordpress. All files are just loaded as is at execution time (ie when the HTTP request is processed by the server). You just have to verify that your plugin is active.

Your curl command is malformed. Use this instead :

curl -X POST http://localhost/wordpress/wp-admin/admin-ajax.php -d action=my_ajax -d data=banjometer --trace -

But CURL is not really the easiest debug tool to use. When I need a fast and easy way to execute some debug php code inside Wordpress, I use the plugin activation hook :

<?php

register_activation_hook(__FILE__, 'my_plugin_debug'); 
function my_plugin_debug() {

    $log = 'banjometer';

    die("OK, $log");
}

?>

Make a new plugin that contains only this code, then try to activate it in the backend. You will get an error (because of the die statement) so the plugin will not be activated, but your code will be run and you can see the log in the error message.

share|improve this answer
    
> You have nothing specific to do to "load" modified php code in Wordpress. All files are just loaded as is at execution time... So if I edit a file and then externally submit an http request direct to the ajax handler it will automatically reload my changed file before executing the code? What do you mean by 'execution time'? –  Gordon Guthrie Jan 21 '13 at 19:54
    
Yes, changes are immediately taken into account. By "at execution time", I mean "at the time you execute the http request". In fact, the entire Wordpress code is loaded and executed at each HTTP request. There is no "place" where the code is staying and "reloaded". –  Fabien Quatravaux Jan 21 '13 at 20:48
    
@GordonGuthrie: I found why you CURL command is not working. See my answer edits for details. –  Fabien Quatravaux Jan 21 '13 at 20:52
    
Cheers Fabien - I (eventually) worked that out myself as well by writing debugging lines into WordPress core. I'm getting there. –  Gordon Guthrie Jan 22 '13 at 14:11
add comment

I don't know anything about CURL, but looking at admin-ajax.php you get a 0 either at the beginning because no action is set, or at the end of the file because it couldn't find a matching action. Perhaps you are executing your CURL command too early? Ie, before the ajax callbacks have a chance to be recognized? If you're on a local install, you could tweak the error codes in admin-ajax.php to at least tell you at which point it is failing. Just don't forget to put it back or you'll pull your hair out later. (Have done)

Just noticed the similar questions in the sidebar and I suspect this is the answer: http://wordpress.stackexchange.com/a/70407/6477

Edit The codex confirms that plugin files are run before the wp_ajax_ hook so how about trying running your code at a later action?

function wpa82473(){
  curl -X POST http://localhost/wordpress/wp-admin/admin-ajax.php -d '{"action":"my_ajax","data":"banjometer"}' --trace -
}
add_action('init', 'wpa82473');
share|improve this answer
    
> Perhaps you are executing your CURL command too early? Ie, before the ajax callbacks have a chance to be recognized? - I have no idea what that means. The code to handle the request is in the plugin. I run curl. There is no logical connection between them. I am not running curl from a page served by WordPress. I don't know how to check if the ajax callback has been registered. Can I call a PHP fn to do that? –  Gordon Guthrie Jan 21 '13 at 19:06
    
I mean your code is being executed before wp_ajax_my_ajax is defined via the add_action, maybe. I'm theorizing here. Can you edit your question to tell us 1. where you are running this code and 2. if there is any more code around it? –  helgatheviking Jan 21 '13 at 20:11
    
The php code runs in the main plugin file - which I assume is executing when I activate the plugin. That file only contains some require statements and the code shown above. –  Gordon Guthrie Jan 21 '13 at 20:16
    
Plugins run at the plugins_loaded hook. wp_ajax_ runs afterwards, see the Codex Action Reference: codex.wordpress.org/Plugin_API/Action_Reference So, your ajax action isn't defined yet. Try wrapping your code in a function that runs at a later point. I will edit with an example. –  helgatheviking Jan 22 '13 at 1:53
    
helgatheviking - the curl command isn't running inside the php. I am writing an ajax service that lets a remote server see if a user is logged in by using a redirect - curl simulates that. I type the curl command at the command line. The problem turned out to be the fact that admin-ajax doesn't expect json in the format I was putting it in - still not sure what format it does expect - but making progress. –  Gordon Guthrie Jan 22 '13 at 14:09
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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