I am having trouble debugging an issue with a plugin that I'm writing. I want to do an Ajax request when a user changes the option on a select input. The thing is it was fully working as expected yesterday, then when I test the website today (no code changes were made at all since yesterday) I'm getting an:

domain.com/wp-admin/admin-ajax.php 400 (Bad Request)

message in my Chrome console. If I try in Internet Explorer/FireFox it doesn't give the error in console but the Ajax request is still not working.

The plugin I'm creating is on a DigitalOcean droplet which was made using their one click app for WordPress.

Here is the barebones code I'm using on the plugin for the ajax:

//This is in a class' constructor method
add_action('wp_enqueue_scripts', array($this, 'plugin_prefix_scripts'));

//PLUGIN_URL is a constant created earlier in the plugin
public function plugin_prefix_scripts() {
wp_enqueue_script('plugin_prefix_scripts', PLUGIN_URL . 'scripts/frontend_script.js');
wp_localize_script('plugin_prefix_scripts', 'js_object',
    'ajax_url' => admin_url('admin-ajax.php')



$('#plugin-prefix-option-one, #plugin-prefix-option-two').on('change', function() {
    var data = {
        'action': 'plugin_prefix_page_change',
        'option_val': 'some_val',

    $.post(js_object.ajax_url, data, function(response) {

Back in the class:

add_action('wp_ajax_plugin_prefix_page_change', array($this, 'plugin_prefix_page_change'));

public function plugin_prefix_page_change() {
    $arr = array(1, 2, 3, 4, 5);


    //I've also tried wp_die() and no die call at all but same outcome

I'm unsure how to debug this further. Any help is much appreciated.


  • 1
    Is this happening when you're logged in or out? You also need to hook wp_ajax_nopriv_plugin_prefix_page_change if you want it to work for logged-out users. Mar 9, 2018 at 15:19
  • (It having stopped working would make sense if you'd been logged out in the meantime and this wasn't hooked) Mar 9, 2018 at 15:25
  • As you said it was only happening on logged out users and using wp_ajax_nopriv fixed the issue. Thank you for saving me a headache.
    – thairish
    Mar 9, 2018 at 15:33

1 Answer 1


As Jacob Peattie mentioned this issue was occuring on logged out users and his suggestion of hooking wp_ajax_nopriv fixed the issue:

add_action('wp_ajax_nopriv_plugin_prefix_page_change', array($this, 'plugin_prefix_page_change'));
  • I am facing the same issue. In which file do I add this line of code? Jul 31, 2020 at 10:57
  • indeed wp_ajax_ and wp_ajax_nopriv_ should be the prefixes Sep 23, 2020 at 17:36

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.