WordPress frontend AJAX requests returning 404 while admin AJAX works fine

I’m dealing with a tricky issue on my WordPress site. The frontend AJAX calls are failing with 404 errors, but everything works fine in the admin area. This problem is affecting both logged-in and guest users.

Here’s what I’ve tried so far:

  • Double-checked all AJAX code (it follows WP docs)
  • Replaced core WP files
  • Disabled plugins one by one

Nothing has fixed it. The weird part is that it was working fine for months and just stopped suddenly. There are no helpful error messages in the logs either.

My AJAX setup looks like this:

add_action('wp_ajax_get_stuff', 'fetch_stuff');
add_action('wp_ajax_nopriv_get_stuff', 'fetch_stuff');

function fetch_stuff() {
    check_ajax_referer('ajax-secret', 'nonce');
    echo 'Data here';
    wp_die();
}

wp_enqueue_script('my-script', 'path/to/script.js', array(), null, true);
wp_localize_script('my-script', 'ajaxData', array(
    'url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('ajax-secret')
));

Any ideas on what could be causing this strange behavior?

hey there, have u checked ur .htaccess file? sometimes it can mess with ajax requests. also, try clearing ur browser cache and wp cache if ur using any. i had a similar issue once and it turned out to be a caching problem. worth a shot!

It’s possible your issue stems from a recent server configuration change or an update to your WordPress core, theme, or plugins. Have you checked your server’s error logs for any clues? Also, consider temporarily switching to a default WordPress theme to rule out theme-related conflicts. If the problem persists, you might want to investigate your site’s URL structure and ensure that your permalinks are set correctly. Sometimes, a mismatch between the expected and actual URL structure can cause AJAX requests to fail. Lastly, have you tried using a network monitoring tool like Chrome DevTools to inspect the failing requests? This could provide valuable insights into the exact nature of the 404 errors.

hmm, interesting problem! have u looked into ur server’s mod_security rules? they can sometimes block ajax requests unexpectedly. also, what about trying a different ajax url like site_url(‘/wp-admin/admin-ajax.php’)? might be worth a shot. let us know what u find!