Displaying PHP Errors from admin-ajax.php in Wordpress 4.9+
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!
Edit wp-includes/class-wp-fatal-error-handler.php
and change this in display_default_error_template( $error, $handled )
:
$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.
Better Solution
Enable PHP Error Output before the error is occurring
Add this to enable PHP Error Output
@ini_set( 'display_errors', 1 );
Eg: At the start of the ajax callback function in this case
Tip:
You can use the Preview tab next to the Response tab in the Dev Tools Network to render the HTML in the Error output. So that you can see the Error without the HTML
Explanation
I don't know why WordPress does not allow enabling this error output but I don't think editing the WordPress Core file and undoing them after debugging is not a good idea as answered here
From that answer, I found that we just need to set the display_errors
PHP init setting to true
or 1
in order to output the errors.
So why don't we just set this wherever we want this to work so that this display_errors
will be enabled for that scop only.
There is no problem if you didn't undo this unlike the other answer