REST API & Developer PHP Intermediate

Log Errors to a Private File

Turn on WordPress debug logging, hide errors from visitors and write the log to a folder that can't be opened in a browser.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 6 lines
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', '0' );

// One folder above the site root, so the log can't be opened in a browser.
define( 'WP_DEBUG_LOG', dirname( __DIR__ ) . '/logs/wp-errors.log' );

Where to add this

Recommended wp-config.php Above the line that says "That's all, stop editing!" in your wp-config.php file.

Turning on WP_DEBUG_LOG the usual way writes errors to wp-content/debug.log, which anyone can download if they guess the address. Logs can contain file paths, database details and other information attackers find useful. This setup keeps the log out of reach.

How it works

WP_DEBUG turns on error reporting, and WP_DEBUG_DISPLAY with display_errors set to 0 stops errors appearing on pages.

Setting WP_DEBUG_LOG to a file path, instead of true, tells WordPress where to write. dirname( __DIR__ ) is the folder above the one containing wp-config.php, which on most hosts is outside the public web root.

Things to watch

  • Create the logs folder first and make sure PHP can write to it.
  • Leave debug logging on only while you need it. Logs can grow large on busy sites.
  • On some hosts the folder above wp-config.php isn’t writable. Ask your host for a private path.

How to test it

Add trigger_error( 'test', E_USER_NOTICE ); to a snippet, load a page and check the log file. The page itself should show no error.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only