2

I'm writing a plugin. I want to run the method get_my_option when someone pressing a button in the settings page of my wordpress plugin. The ajax call is made but the method never runs.

In the page I have the following code:

<div class="wrap">
<input type="button" value="test" onclick="my_js_function();"/>
</div>
<?php
wp_enqueue_script( 'function', plugins_url( '/js/synchandler.js',__FILE__),         array('jquery'));
wp_localize_script( 'function', 'my_ajax_script', array( 'ajaxurl' => admin_url(  'admin-ajax.php' ) ) );


function get_my_option()
{
    //do something
    die();
}

add_action("wp_ajax_nopriv_get_my_option", "get_my_option");
add_action("wp_ajax_get_my_option", "get_my_option");
?>

The added synchandler.js file:

function my_js_function() 
{
    jQuery.ajax({
    url: my_ajax_script.ajaxurl,
    type: 'POST',
    data: ({ action: 'get_my_option', affiliate: 'daisycon' }),
    success: function (response) {
        console.log("got this: " + response);
        }
    });
}

There is a response but that's a 0, so the javascript is added nicely but that's about it. I'm getting no errors in my log by running this. AjaxURL is added in the page (seems to be a common mistake).

/* <![CDATA[ */
var my_ajax_script = {"ajaxurl":"http:\/\/mywebsite.net\/wp-admin\/admin-ajax.php"};
/* ]]> */

The button to trigger it all uses onclick="my_js_function(); on the HTML button-tag.

As easy as it is, the method get_my_option doesn't get triggered by the ajax call. What am I doing wrong here?

4
  • Ehm... you need to close PHP with ?> before adding JavaScript code and opening it again afterwards: <?php.
    – kaiser
    Commented Apr 22, 2014 at 10:51
  • All javascript is in a separate file and added with wp_enqueue_script() so don't think that's a problem.
    – JennisDev
    Commented Apr 22, 2014 at 12:05
  • 1
    "The added synchandler.js file:" is the part I'm referring to. If this is the contents of the JavaScript file, then please show us where you call that function. I can't see it triggered anywhere.
    – kaiser
    Commented Apr 22, 2014 at 12:20
  • It's inside a html input tag where I'm using an onclick event. Gonna try to put it in the question (is now written below the last codesnippet).
    – JennisDev
    Commented Apr 22, 2014 at 12:29

2 Answers 2

7

Where are you calling add_action()? If it's in a place where you're already outputting HTML, it's too late, and that's probably a place that wont even be looked at during an AJAX request.

You should include that code in your theme's functions.php file, or as early as possible in a plugin.

4
  • As I have just seen that the OP seems to add the code in mid page, I have to double that. +1
    – kaiser
    Commented Apr 22, 2014 at 15:52
  • Indeed it is. Adding the actions in the functions.php made it work. The problem I have with adding it in the functions.php in my theme folder is that I can't access the classes I need in the plugin folder. A solution is taking all code to the theme but it's kinda making the plugin useless.
    – JennisDev
    Commented Apr 22, 2014 at 16:47
  • Then just put it at the start of your plugin, outside of any other functions or classes and before you start outputting anything.
    – Dave Ross
    Commented Apr 23, 2014 at 2:26
  • Adding it to the start of the plugin fixed it. Thanks!
    – JennisDev
    Commented Apr 23, 2014 at 7:41
2

You'll need to put your functional code into the plugin, as a function (This depending on your way of developing your plugin.

I prefer to add the add_action calls to admin_init - It's called with admin-ajax.php too.

wp_enqueue_script( 'function', plugins_url( '/js/synchandler.js',__FILE__),         array('jquery'));
wp_localize_script( 'function', 'my_ajax_script', array( 'ajaxurl' => admin_url(  'admin-ajax.php' ) ) );


function get_my_option()
{
    //do something
    die();
}
function add_ajax_actions() {
    add_action("wp_ajax_nopriv_get_my_option", "get_my_option");
    add_action("wp_ajax_get_my_option", "get_my_option");
}
add_action( 'admin_init', 'add_ajax_actions' );

I don't know if this is best practice - It's purely a personal preference, to avoid adding hooks to every page loaded - Only to admin pages.

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.