0

I have read other questions regarding the same issue but nothing seems to help in my case. Maybe someone sees something in my code that I cannot see?

I created a plugin with a class, then I have an AJAX call that triggers when I click on a HTML element on the page. All seems to work, except that it doesn't enter the callback inside the plugin.

plugin.php

class BSPFPluginClass {
  protected $nonce = 'mynonce';

  public function __construct() {
    /** other actions here ... */
    add_action( 'wp_enqueue_scripts', array( $this, 'myinit' ) );
    add_action( 'wp_ajax_myajaxcall', array( $this, 'myajaxcall' ) ); // executed when logged in
    add_action( 'wp_ajax_nopriv_myajaxcall', array( $this, 'bspf_ajax_voting' ) ); // executed when logged out
  }

  public function myinit() {
    // Register the script
    wp_register_script( 'plugin-js', plugins_url( 'js/plugin.js', __FILE__ ), array() );

    if (is_page('somepage')) {
      // Enqueued script with localized data.
      wp_enqueue_script( 'plugin-js' );
      // Localize the script with new data
      wp_localize_script( 'plugin-js', 'plugin_ajax', [
        'ajax_url' => plugins_url( 'plugin.php', __FILE__ ), 
        'nonce'    => wp_create_nonce( $this->nonce ),
      ]);
    }
  }

  public function myajaxcall() {
    check_ajax_referer( $this->nonce );
    if (true) {
      echo 'Yes it works!';
    }
    else {
      echo 'No it does not work!';
    }
    wp_die(); // stop executing script
  }
}

Then, js/plugin.js

jQuery(document).ready(function($) {
  $(".myclass").click(function() {
    $.post(plugin_ajax.ajax_url, {
      _ajax_nonce: plugin_ajax.nonce,
      action: 'myajaxcall'
    }, function(response) {
      console.log('SUCCESS: '+response);
    }).done(function() {
      console.log('DONE');
    }).fail(function() {
      console.log('ERROR');
    }).always(function() {
      console.log('ALWAYS');
    });
  });
});

When I hit the HTML element with the trigger class everything goes well and I get the following on the console:

SUCCESS: 
DONE
ALWAYS

So it is like everything is correct, except is not entering the action 'myajaxcall'.

Any leads?

Thank you in advance.

2
  • 2
    The ajax_url should be admin_url( 'admin-ajax.php' ) and not plugins_url( 'plugin.php', __FILE__ )
    – Laxmana
    Apr 13, 2017 at 16:03
  • @Laxmana you should post that as an answer
    – Tom J Nowell
    Apr 13, 2017 at 16:14

1 Answer 1

3

The ajax_url should be admin_url( 'admin-ajax.php' ) and not plugins_url( 'plugin.php', __FILE__ ) when you localize your js variables.

5
  • Thank you @Laxmana , actually, the AJAX is not for an admin page but rather a public page, so I don't see why I would need to add the admin_url( 'admin-ajax.php' )
    – Dani
    Apr 13, 2017 at 17:10
  • 1
    No. admin-ajax.php is a WordPress Core file that handles Ajax Requests. It's inside wp-admin folder. WordPress acts like a controller that handles Ajax requests and executes the appropriate action of the request. I suggest reading more about Ajax in WordPress here
    – Laxmana
    Apr 13, 2017 at 17:11
  • 1
    It doesn't matter if the Ajax call is being made from a public page or a admin page or even a different site. All Ajax calls should be made at admin-ajax.php for your hooked action to be executed.
    – Laxmana
    Apr 13, 2017 at 17:13
  • Thank you @Laxmana, this is the right answer. It works like a charm. I hadn't read that article you shared and it really helped me. Thank you!
    – Dani
    Apr 14, 2017 at 7:54
  • @Dani You are welcome! If you are satisfied with the answer please accept it.
    – Laxmana
    Apr 14, 2017 at 10:22

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.