In this tutorial we can learn how to enable PHP error log for WordPress sites.
WordPress is the most popular free and open-source content management system which is based on PHP and MySQL. WordPress is used by more than 25% of the 10 million websites. A system admin will eventually have to work with a WordPress site. For an admin, error logs are clues which can be retrieved from apache log. However, WordPress comes with a specific debug systems designed to simplify the process as well as standardize code across the core, plugins, and themes. This guide will teach you how to enable the debug option for a WordPress site and various configurations.
Editing the configuration file for Debugging
The basic configuration file of WordPress is the wp-config.php file. We have to make some configuration changes to enable debugging in this file. The WP_DEBUG is the variable used for enabling debugging. It is a PHP constant (a permanent global variable) that can be used to trigger the “debug” mode throughout WordPress. It is assumed to be false by default and is usually set to true in the wp-config.php file on development copies of WordPress
define( ‘WP_DEBUG’, true );
define( ‘WP_DEBUG’, false );
Enabling this will result all PHP errors, notices, and warnings to be displayed in the website. To save the errors to a location WP_DEBUG_LOG is used. By configuring this option, we can save the error to a debug.log file in the wp-content directory in the Webroot.
define( ‘WP_DEBUG_LOG’, true );
To restrict the errors that appear in the website, we have a WP_DEBG_DISPLAY option. Making this option false will let you hide the messages. The default is ‘true’ which shows errors and warnings as they are generated. Setting this to false will hide all errors. This should be used in conjunction with WP_DEBUG_LOG, so that errors can be reviewed later.
define( ‘WP_DEBUG_DISPLAY’, false );
An example configuration of error log on a WordPress site:
/**
* This will log all errors notices and warnings to a file called debug.log in
* wp-content (If Apache does not have write permission, you may need to create
* the file first and set the appropriate permissions (i.e. use 660) )
*/
define( ‘WP_DEBUG’, true );
define( ‘WP_DEBUG_LOG’, true );
define( ‘WP_DEBUG_DISPLAY’, false );
The default debug log file is /wp-content/debug.log. Placing error logs in publicly accessible locations is a security risk. Ideally, your log files should be placed above your site’s public root directory. If you can’t do this, at the very least, set the log file permissions to 600 and add this entry to the .htaccess file in the root directory of your WordPress installation.
<Files debug.log>
Order allow,deny
Deny from all
</Files>
This prevents anyone from accessing the file via HTTP. You can always view the log file by retrieving it from your server via FTP.
There are other debugging plugins available which will show more information about the internals. Query Monitor, Debug Bar, Log Deprecated Notices are just some examples. Enabling this plugin can make debugging straightforward.
If you need any further help please contact our support department.