How to Turn On Error Logs in WordPress
Although WordPress is relatively easy to manage, troubleshooting errors are quite a common issue for its users. Dealing with them usually isn’t difficult – all it takes is a little bit of practice. The first step of every WordPress error troubleshooting is retrieving the error log.
The error log contains all errors, warnings, and notices about the code behind your site. It can sometimes help you pinpoint the exact file and line of code that are causing the issue. The error log can also provide clues for solving the seemingly insoluble errors, such as “the white screen of death”.
Making sense of the error log, however, can be quite challenging if you are not tech-savvy. To become tech-savvy, you have to start somewhere – so let’s begin by showing you how to turn on and acquire the error log in WordPress.
How to Set Up WordPress Error Logs In Your WP-Config File
The wp-config.php file is an important WordPress configuration file. By slightly editing it, you will enable WordPress default debugging and show errors. You can do this via an FTP client or via cPanel.
Using an FTP Client
A prerequisite for performing any edits on your server files is to set the file permissions properly. The suggested permissions on the wp-config.php file are 440 or 400. This means you’re only permitted to read the files. To be able to acquire an error log, you need to change the file permissions temporarily to 644 or 666. For clarification,644 gives reading and writing/editing permissions to admin users, while the rest of users get only reading permissions. The 666 value gives both of the aforementioned types of permissions to all users.
To proceed you need an FTP client and the knowledge of editing the wp-config.php file. Start by connecting to the server using your FTP credentials. Navigate to your root WordPress directory. Locate the wp-config.php file, right-click on it and select the “File permissions” option.
Manually insert one of the suggested numeric values (644 or 666) and click on “OK”.
After changing the permissions, go back to the root directory, right-click on the wp-config.php file and select “View/Edit”.
Open the file using a text editor and insert this line of code:
define('WP_DEBUG', true);
above the /* That’s all, stop editing! Happy blogging. */.
If the define('WP_DEBUG', false);
line is already present, only change false into true.
Save the changes and upload the edited file to your root WordPress directory. This will override the file that is currently on your server.
Via cPanel
You can also use cPanel to edit the wp-config.php file. Log in to the cPanel using your credentials and click on the “File Manager”.
Navigate to the root directory of your WordPress installation on the left, locate the wp-config.php file, right-click it and select “Change Permissions”.
Make sure to enable both reading and writing permissions for the (admin) user by ticking off the appropriate checkbox. Then, click on “Change Permissions”.
Afterward, right-click on the wp-config.php file once more and select “Edit”.
The file will open in the default cPanel editor, so you only need to add the define('WP_DEBUG', true);
above the /* That's all, stop editing! Happy blogging. */
line of code within the wp-config file.
If the file already contains the following line define('WP_DEBUG', false);
simply change the false to true and click on “Save Changes” in the top-right corner of your screen.
How to Manage Displaying Errors
Once you successfully display the errors, they will show on your pages, both on the backend and frontend. Unless your site is in development, exposing the errors live clearly isn’t a good idea. Instead, you should save them as a single file and hide them from your site. This will allow you to view the file later and troubleshoot errors at any time.
To do this, add this code to your wp-config.php file.
define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false ); @ini_set( 'display_errors', 0 );
Add the code below the following line define('WP_DEBUG', true);
and above the /* That's all, stop editing! Happy blogging. */
line of code.
You can edit the file using an FTP client or via cPanel, following the same steps we laid out in the previous section. After the edit, your wp-config.php file should look like this.
By adding define( ‘WP_DEBUG_LOG’, true ); we force WordPress to store all the error messages into one file. By default, that file is called debug.log and it is located in your-WordPress-root/wp-content/ directory. You can also store error messages in some other file that you created. In that case, change the true from “define( ‘WP_DEBUG_LOG’, true );” to the path of the file. For example:
define( 'WP_DEBUG_LOG', path-to-your-site/wp-content/error.log );
Furthermore, WP_DEBUG_DISPLAY determines whether the error messages are shown or hidden on the screen. By adding define( ‘WP_DEBUG_DISPLAY’, false ); you tell WordPress to hide the messages from your screen, but they are still printed to the designated file. If you also add @ini_set( ‘display_errors’, 0 ); this will disable error printing for your PHP, making sure that users will not be able to see them on the frontend.
Acquiring the Error Log
With the errors shown and successfully written in the appropriate error log file, the only thing that remains is to review the error messages and take further steps.
Manually Downloading debug.log File
To download the log file connect to your server, navigate to your root WordPress folder and click on wp-content. Find the debug.log file inside, right-click on it and press “Download”.
Save the file on your desktop. Then, make sure to revert the previous edits. This includes removing the code you inserted and changing the permissions on the wp-config.php file to 440 or 400. If you want, you can do this after solving the errors.
Finally, open the saved debug.log file with a text editor and check the error messages written. Depending on your level of expertise, you can either solve the errors yourself or hire a developer.
More Advanced Debugging Features
We will also take a look at some more advanced debugging possibilities. The following code should be added in wp-config.php, above the /* That's all, stop editing! Happy blogging. */
line.
This code forces WordPress to use un-minified versions of CSS and JS files, which is useful for debugging changes and/or errors found specifically in your CSS and JS files.
By setting CONCATENATE_SCRIPTS constant to false, you force WordPress to load all scripts separately. This can prove useful for pinpointing faulty/incompatible scripts.
If you are experiencing database issues, analyzing the queries that are run is a good way to start debugging. By inserting the code above you will put the query content, which function called it and for how long it was run in $wpdb->queries array. This will allow you to dive deeper into the backend portion of the theme’s code and what it is doing exactly.
You can then add the following code to show all the queries in a readable form and analyze them.
global $wpdb; print("<pre>".print_r($wpdb->queries,true)."</pre>");
However, keep in mind that this code will affect your site’s performance, so we recommend using it for debugging purposes only.
Final Thoughts
Following this guide, you can show and safely store error logs in a file, change file permissions, and run some debugging features intended for more advanced WordPress users. This tutorial will also help you learn more about the way your website works – so keep it close because you could need these instructions in the future.