0

I have built a plugin and I need to use ajax in the admin and have followed the documentation here: https://codex.wordpress.org/AJAX_in_Plugins

However, the response I keep getting is 0.

I know there are many posts with the same issue and I have been through them all but cannot find the solution for me. Please help.

I use a class for the plugin and have the jquery in a seperate file.

myplugin-admin.js

function getFeatureIDs(selectObject){
    var value = selectObject.value;  
    console.log(value);
    console.log(ajax_object.we_value);
    console.log(ajax_object.ajax_url);
    var data = {
        'action': 'get_etim',
        'whatever': ajax_object.we_value   
    };
   
    
    jQuery.post(ajax_object.ajax_url, data, function(response) {
        alert('Got this from the server: ' + response);

    });

}

class-myplugin-admin.php

class myplugin_Admin {
    private $myplugin;
    private $version;

    public function __construct($myplugin,$version ) {
        
        add_action('wp_ajax_get_etim', 'get_etim');
        add_action( 'wp_ajax_nopriv_get_etim', 'get_etim' );
    }

    public function enqueue_scripts() {

        wp_enqueue_script( $this->myplugin, plugin_dir_url( __FILE__ ) . 'js/myplugin-admin.js', array( 'jquery' ), $this->version, false );

        wp_localize_script( $this->myplugin, 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    }

    public function get_etim() {

        echo $_REQUEST['whatever'];

        wp_die();
    }
}

Results in console:

EC003025
1234
https://mytestsite.plesk.page/wp-admin/admin-ajax.php

Alert is always,

"Got this from the server: 0"

which suggests the action function is not being called.

Please, what am I missing?

Thanks

1
  • this is likely a common mistake, is there a reason you're using the old legacy API instead of the new API? Modern AJAX is done via the REST API and register_rest_route. Also the Codex is very old and contains out of date information. You should be using the developer hub instead
    – Tom J Nowell
    Mar 3, 2022 at 11:15

1 Answer 1

0

This is because you chose to use a class but did not change the add_action to account:

add_action( 'wp_ajax_nopriv_get_etim', 'get_etim' );

That is not how you hook a class function to an action or filter. So your Javascript is correct, but your handler is not connected to the hook. That is why your AJAX handler fails, incorrect use of add_action.

The solution here is the same as the other questions on the site asking how to hook a class function.

General notes:

  • the use of a class does not improve this code, it is not OO
    • ask yourself, if it's meant to call get_etim, which get_etim? a class can be turned into an object multiple times, how would it know which one to call? And if you only create/use it once what was the point of the class
  • this is using the old legacy API, you should use the new REST API for AJAX via register_rest_route which avoids lots of pitfalls, and gives you a pretty URL to make requests to
  • The codex is old and was retired several years ago, and can contain out of date information. You should refer to the developer page instead at https://developer.wordpress.org/
4
  • Thanks for the response. However, Im not clear what the solution is. Its the first time I have built a plugin and have not used used Ajax on wordpress for years. I have been following a tutorial for the plugin which recommends to download base files that use classes. Im too far into the project to start changing this now.
    – LeeTee
    Mar 3, 2022 at 13:24
  • There are lots of questions on here where people tried to add a class function to a filter and it didn't work, making the exact same mistake you did. The problem isn't an obscure WordPress issue but a misunderstanding of what add_action does and what a callable type value is in PHP. In particular, it's the very first and top most voted comment on the add_action documentation: developer.wordpress.org/reference/functions/add_action/…
    – Tom J Nowell
    Mar 3, 2022 at 14:14
  • Also more information about what a callable is: php.net/manual/en/language.types.callable.php The tutorial you're following will also be using the solution, if it does not then it will never work and has never been tested. I would be very suspicious of the tutorial you are using
    – Tom J Nowell
    Mar 3, 2022 at 14:17
  • Yes, finally something is working. Thankyou. So I changed add_action to 'add_action( 'wp_ajax_get_etim', array( $this, 'get_etim' ) ); ' However, I am now struggling to understand how to get the 'selectObject.value;' to the script using the 'wp_localize_script()'. I will also look into the rest API for ajax as soon as I get this working.
    – LeeTee
    Mar 3, 2022 at 15:15

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.