WordPress is one of the most popular Content Management Systems (CMS) available in the market today. It is built upon various development languages, with PHP being the backbone. Whenever a user visits a WordPress site, the PHP code is compiled by a PHP module installed on the server (Apache or Nginx). This essentially means lot of PHP errors might occur during processing of the user’s requests. If they do, it indicates that there is a problem with the PHP code. Therefore, you have to understand the basic knowledge of how to check these PHP errors before looking for a solution. In this article, we will explain how to debug PHP errors in WordPress site along with enabling error logging.
PHP Errors in WordPress
PHP errors can be a warning or fatal error that break your site. These problems can happen due to different reasons like below.
- Omitting a semicolon in the syntax
- Invoking the wrong variable
- Clashing with another plugin or even a server parameter
- Incompatible plugin or theme with the latest PHP version
Do not get panic when you see errors on the browser frontend or in the admin panel. Since, you have to update core WordPress, PHP version, theme and plugins frequently it is common to get errors due to compatibility and other reasons.
Why You Should Monitor PHP Errors?
Keeping a close eye on your site’s PHP errors is critical in two major aspects.
- Security – information in PHP errors can expose your site to malicious attacks. Hence, understanding and fixing the errors is a must task for maintaining a safe and secure website.
- Optimization – PHP errors can drag down the speed of your site and if these issues go unnoticed, they can degrade performance and waste bandwidth.
Although many installations do not provide an error notice for these issues by default, PHP problems frequently happen unexpectedly and without warning. Even minor modifications on your website can trigger them to your server settings, database setup, or WordPress files.
The truth remains, even though your site looks to be functioning normally on the surface, it could be suffering from unnoticed PHP issues that need to be addressed immediately. And because of that, WordPress administrators should keep an eye on PHP errors.
Debug PHP Errors in WordPress
This article will look into how to debug PHP errors in WordPress using two ways.
- Debug PHP errors manually with a code.
- Debug PHP errors using plugins.
If you are a developer or have a good grasp of one or two programming languages, this method will be much easier. It might not be the fastest since you might get down writing some lines of code, but even that makes it more interesting. For this particular post, we will edit the code on Visual Studio Code as the site is locally hosted on LAMPP. If your site is already in production, you can edit the code via File Manager app or using FTP.
We will look at three main PHP constants:
|WP_DEBUG||Enable debugging errors|
|WP_DEBUG_DISPLAY||Display the errors in the browser frontend|
|WP_DEBUG_LOG||Enable logging of errors in a log file|
In our test site, right now there are no PHP errors displayed on the WordPress site, as shown in the image below. That’s because by default WordPress prevents showing errors in the browser by setting the WP_DEBUG parameter to ‘false’.
To set WordPress debugging to true, go to the root installation folder of your site and find the wp-config.php file. Edit the file and check whether it contains the following line.
define( 'WP_DEBUG', false );
It should look like as shown in the image below.
It the line is there, simply change the WP_DEBUG value to true. Otherwise, you can insert a new line with WP_DEBUG value as true. In addition, add the below line to enable displaying PHP errors in browser.
define( 'WP_DEBUG_DISPLAY', true );
With both parameters enabled, it should look like as shown in the image below.
Save the file to apply the changes and upload back on your server. Now when we reload our site, we can see the errors displayed on the screen. As you can see, it is a warning message mentioning the theme has syntax issue which could cause error in future PHP version.
Note that some PHP errors appear in localhost environment which will not be applicable to live sites. Also, make sure to add the codes in wp-config.php file just above the line saying, “/* That’s all, stop editing! Happy publishing. */”.
Unfortunately, these errors appear on the browser that wouldn’t be quite professional. Additionally, some errors might reveal vulnerabilities and security flaws present on the site. A reliable solution would be to create a log file where we would dump all these errors.
But first, remember to turn off the error display by editing the WP_DEBUG_DISPLAY value as shown in the image below. Leave WP_DEBUG value as true so that we can be able to log the errors.
Below are some of the situations you can create a WordPress error log file:
- Your entire website went down
- Notice a blank screen when trying to open the site
- Having trouble with your website’s performance
- Warning with HTTP status codes 401, 404 or 500 occurs
- If a plugin or theme isn’t functioning correctly. You might also consider creating the error logs to get more details of the issue.
To dump WordPress errors to a log file, open the wp-config.php file and look for the below line.
define( 'WP_DEBUG_LOG', true );
If it does not exist, you can insert a new line. Sometime the line is also commented and make sure to remove the comments and set the WP_DEBUG_LOG value to true, as shown in the image below.
Save the file and upload back to your server. Now, reload the website on browser. WordPress will create a log file called debug.log in the wp-content directory, where it dumps all the PHP errors on your site.
When we open the file, you can see it contains all the errors that were being displayed on our site.
This can come quite in handy, especially when you are working on a live environment and you wouldn’t want users to see errors on their screens. You can then check and correct these errors behind the scenes without affecting real users.
Though the above method is easy, you need to access the server from hosting account or remotely using FTP for editing wp-config.php. Fortunately, you can debug PHP errors in WordPress using different types of plugins. Here we will discuss the following two plugins:
- WP Debugging plugin
- Query Monitor plugin
Debug PHP Errors using the WP Debugging plugin
The WP Debugging plugin is a free WordPress plugin that launches WordPress debug mode and enables error logging. When activated, this plugin sets the debug constants in wp-config.php and removes them when deactivated. A PHP exception is raised if there are any issues. These constants include:
define( 'WP_DEBUG'; true ); define( 'WP_DEBUG_LOG'; true ); define( 'SCRIPT_DEBUG'; true ); define( 'SAVEQUERIES'; true );
When the plugin is deactivated, every effort is taken to restore the state of pre-existing constants. Although, the default settings and any saved settings are restored when the plugin is activated again. The plugin also comes with a debugging section where you can configure your debugging constants. To get started;
- Go to the WordPress dashboard and click on the ’tools’ menu
- Click on the ‘WP debugging’ submenu.
- Scroll down and select the debugging constants that you want to apply on your site.
- Click the ‘Save Changes’ button to apply the changes, as shown in the image below.
Debug PHP Errors Using Query Monitor Plugin
Query Monitor is a free WordPress plugin that aids web admins in analyzing frontend and backend page queries. It will also alert you to significant PHP bugs among its many uses. You will see a new item in the top admin bar after installing and activating it. When it identifies a critical (PHP) issue, the toolbar will be in red as shown in the image below.
This toolbar contains a dropdown menu with several options that you can use to manage your WordPress site. However, in this post, we will only focus on the PHP errors. Hover your mouse over the newly introduced Query Monitor toolbar and click PHP Errors. That will open a window displaying all the PHP errors on your WordPress site, as shown in the image below.
Additionally, you can set an authentication cookie that allows you to view Query Monitor output when you’re not logged in or when you’re logged in as a different user. To do this, follow the steps below:
- Navigate to the “Plugins” section on your WordPress dashboard
- Click on the “Settings” option below the Query Monitor plugin
- A developer console window will pop up on your admin panel
- Click “Set authentication cookie”.
This post has given you the various methods you can use to debug PHP errors on your WordPress website. If you are well-versed in programming, you should definitely try out the manual way of interacting directly with your WordPress code. However, be careful when editing files and not to create any more errors. If you want a quick and straightforward approach, the plugins will significantly come in handy.