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

262

We've got some custom endpoints set up that do various things, which we access via/wp/wp-admin/admin-ajax.php?action=some_action

However whenever there is an error as we're developing, such as syntax, logical, fatal etc, we simply get "500 Internal Server Error" when viewing the page in the browser.

Every other page on the site when there's an error, it gives us the PHP error, to help us debug.

However when the error comes from theadmin-ajax.php area, we have to then open our PHP Log file to see the error instead - which is more of a pain when actively developing

Is there something in wordpress that disables displaying of errors on this URL namespace? and if so, how can we prevent this to allow rendering of the errors on the browser?

114

Answer

Solution:

Editwp-includes/class-wp-fatal-error-handler.php and change this indisplay_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 toadmin-ajax.php using Dev Tools.

84

Answer

Solution:

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 seedebug.log underwp-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 methodwp_debug_mode() inwp-includes/load.php

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

People are also looking for solutions to the problem: php - Allowed memory size exhausted while using Yii datatables

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.