WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Fighting to create an ajax request. I've been reading couple of articles but still I can't get my plugin to send and recive ajax datas.

first init the js

    function ajax_load_scripts() {
        // load our jquery file that sends the $.post request
        wp_enqueue_script( "ajax-calls", plugin_dir_url( __FILE__ ) . '/assets/js/ajax_calls.js', array( 'jquery' ) );

        // make the ajaxurl var available to the above script
        wp_localize_script( 'ajax-calls', 'the_ajax_script', 
                array( 'ajaxurl' =>   plugin_dir_url('my_plugin.php') ,
                       '_ajax_nonce' => wp_create_nonce( 'my_ajax_nonce' )
                  ) );
add_action('wp_print_scripts', 'ajax_load_scripts');

than trying to catch posted data in my plugins entry file

function tipps_processing_function(){

    if ($_POST['results']){
        $token = '393a9276ae329e00b3739d2e76e52f3b';
        $tips = json_encode($_POST['results']);
        $save = new Tipspiel();
        $save->save_tips($token, $tips);
        return 'OK';
    //do stuff here

add_action('wp_ajax_nopriv_add_tipps', 'text_ajax_process_request');

my js

$j("form#tiepspiel").submit(function(e) {
        e.preventDefault(); // this disables the submit button so the user stays on the page            
        var str = $j(this).serialize();
        if ($j('ul#sortables li').size() > 10){

            $j( "#dialog-modal" ).dialog({
                    height: 140,
                    modal: true
        var data = {
                action: 'add_tipps',
                tips: str,
                _ajax_nonce: the_ajax_script._ajax_nonce


        $j.post(the_ajax_script.ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
            //$j.cookie("tipspiel", "competitor", { expires: 1 });
           $j('form#tiepspiel').fadeOut('slow', function() {
                // Animation complete.

but I don't see my varibales dumped

share|improve this question
Are you getting any errors in your browsers script console? Is $j your alias for jQuery? – chrisguitarguy Dec 2 '12 at 21:04
firebug shows no errors. alert box is empty – fefe Dec 2 '12 at 21:05
up vote 2 down vote accepted

If you need to use the WordPress enviorment and functions in your ajax processing funciton then you need to post/get your AJAX request to admin-ajax.php, So change

'ajaxurl' =>   plugin_dir_url('my_plugin.php')


 'ajaxurl' =>   admin_url('admin-ajax.php')
share|improve this answer
yes I did realized between thanks a lot – fefe Dec 2 '12 at 22:21

You have this, which will work for not-logged-in visitors:

add_action('wp_ajax_nopriv_add_tipps', 'text_ajax_process_request');

But if you don't have this too, you won't trigger the call for logged-in users (which I presume you are):

add_action('wp_ajax_add_tipps', 'text_ajax_process_request');
share|improve this answer

Most likely, your JS isn't including the 'action' parameter, or it isn't set to add_tipps. Here's an example of the JS you can use, from the codex:

<script type="text/javascript" >
jQuery(document).ready(function($) {
    var data = {
        action: 'add_tipps',
        whatever: 1234
    $.post(ajaxurl, data, function(response) {
        alert('Got this from the server: ' + response);
share|improve this answer

Your Answer


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.