Displaying PHP Errors from admin-ajax.php in WordPress 4.9+

Solution:1

If you want to see the PHP errors when using ajax, open up the wp-includes/load.php file & on line 336, change this line:

if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
        @ini_set( 'display_errors', 0 );
    }

to

if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
        @ini_set( 'display_errors', 1 );
    }

You will now be able to see the ajax errors in your browser console. Remember to undo this when finished debugging!

Solution:2

Edit wp-includes/class-wp-fatal-error-handler.php and change this in display_default_error_template( $error, $handled ) (Line: 193):

$message = sprintf(
                        '<p>%s</p><p><a href="%s">%s</a></p>',
                        $message,
                        /* translators: Documentation explaining debugging in WordPress. */
                        __( 'https://wordpress.org/support/article/debugging-in-wordpress/' ),
                        __( 'Learn more about debugging in WordPress.' )
                );

to

$message = sprintf(
                        '<p>%s</p><p><a href="%s">%s</a></p>',
                        $message . ' ' . json_encode($error),
                        /* translators: Documentation explaining debugging in WordPress. */
                        __( 'https://wordpress.org/support/article/debugging-in-wordpress/' ),
                        __( 'Learn more about debugging in WordPress.' )
                );

Then preview HTML response to admin-ajax.php using Dev Tools.

Solution:3

https://codex.wordpress.org/Debugging_in_WordPress

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings 
define( 'WP_DEBUG_DISPLAY', false );

Define the following constants as such. You will be able to see debug.log under wp-content. I usually leave the debug display off too because it causes the headers already sent problem.

EDIT:

So apparently error reporting is turned off for ajax requests in the last of line of the method wp_debug_mode() in wp-includes/load.php

if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
        @ini_set( 'display_errors', 0 );
    }