I have set up debugging, and using error_log("message"); works fine everywhere I have used it so far, except within an AJAX handler.

How does one log to the error handler within an AJAX handler?

E.g. The following is working and returning status 200, but not logging to the error log:

function my_action_handler() {
    // These are never logged
    error_log("my_action_handler was called"); //This is never logged
    $a = $_POST['a'];

add_action( 'wp_ajax_my_action', 'my_action_handler' );

  • This is probably because that never runs, if you're logged out you need to use a different hook. Instead consider making requests to a REST API endpoint instead, they have fewer hidden pitfalls, pretty URLs, and will handle validation and sanitising for you unlike admin ajax which forces you to write it from scratch. Check out the register_rest_route function – Tom J Nowell Jun 24 '20 at 15:12
  • Thanks for the reply, as mentioned the handler does indeed run, I can alter the received data, and send it back, and see the received data is different. I will check out register_rest_route too, thanks. – run_the_race Jun 24 '20 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.