How to find cause of PHP notices with no stack trace?

You can get the backtrace by trapping the E_NOTICE message. If you add the following snippet to wp-config.php just before

/* That's all, stop editing! Happy blogging. */

you should see the full backtrace in the error log.

set_error_handler(function() {
    error_log(print_r(debug_backtrace(), true));
    return true;
}, E_USER_NOTICE);

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

/* That's all, stop editing! Happy blogging. */

PS: Returning true at the end of the function tells PHP to stop processing the E_NOTICE, which is probably what you want at this point.

I just used this approach on a project and it was exactly the solution I was looking for. In fact, I found this post while looking for a way to do the same thing. Happy sailing!

Leave a Comment