WordPress is one of the most popular content management systems (CMS) available on the market today. It is built on various development languages, which are based on PHP. Every time a user visits a WordPress site, the PHP code is compiled by the PHP module installed on the server (Apache or Nginx). Essentially, this means that a lot of PHP errors can occur during the processing of user requests. If so, it indicates a problem with the PHP code. Hence, you must understand the basic knowledge of how to check for these PHP errors before looking for a solution. In this article, we will explain how to debug PHP errors on a WordPress site and also enable error logging.
PHP errors can be a warning or a fatal error that breaks your site. These issues can occur for a variety of reasons, as listed below.
- Omitting a semicolon in syntax
- Calling the wrong variable
- Conflict with another plugin or even with a server setting
- Incompatible plugin or theme with latest PHP version
Don’t panic when you see errors in the browser interface or in the admin panel. Since you need to frequently update the WordPress core, PHP version, theme, and plugins, errors often occur due to compatibility and other reasons.
Why should you track PHP errors?
Keeping a close eye on PHP errors on your site is critical in two main ways.
- Security – PHP error information can expose your site to malicious attacks. Therefore, understanding and correcting errors is a mandatory task to maintain the security and reliability of a website.
- Optimization – PHP errors can slow down your website speed and if these problems go undetected, they can slow down performance and waste bandwidth.
Although many default installations do not provide notification of these issues, PHP problems often occur unexpectedly and without warning. Even minor changes to your website can cause them to change in your server settings, database setup, or WordPress files.
The truth remains, even if your site looks fine on the outside, it may be suffering from subtle PHP issues that need to be addressed immediately. And that’s why WordPress administrators need to watch out for PHP errors.
Debugging PHP Errors in WordPress
In this article, we will look at how to debug PHP errors in WordPress in two ways.
- Debug PHP errors manually with code.
- Debug PHP errors with plugins.
If you are a developer, or if you are proficient in one or two programming languages, this method will be much easier. It might not be the fastest option since you’ll have to write multiple lines of code, but even that makes it more interesting. In this particular post, we will be editing the code in Visual Studio Code since the site is hosted locally on LAMPP. If your site is already live, you can edit the code through the File Manager app or FTP.
|WP_DEBUG||Enable debug errors|
|WP_DEBUG_DISPLAY||Displaying Errors in the Browser Web Interface|
|WP_DEBUG_LOG||Enable logging errors to a log file|
On our test site right now, the WordPress site does not display PHP errors as shown in the image below. This is because, by default, WordPress prevents errors from showing up in the browser by setting the WP_DEBUG option to false.
Save the file to apply the changes and upload back to your server. Now when we reload our site, we can see the errors displayed on the screen. As you can see, this is a warning message that mentions that the theme has a syntax issue that may cause an error in a future version of PHP. Please note that some PHP errors appear in the localhost environment that do not apply to live sites.
Unfortunately, these errors appear in the browser, which would not be entirely professional. In addition, some errors may reveal vulnerabilities and security flaws present on the site. A reliable solution would be to create a log file in which we would dump all these errors.
- Your entire site is down.
- Notice the blank screen when trying to open the site
- Problems with the performance of your site
- You receive a warning with HTTP status codes 401, 404, or 500
- If a plugin or theme is not working correctly. You may also want to consider creating error logs to get more details about the problem.
To write WordPress errors to a log file, open the wp-config.php file and look for the line определить ('WP_DEBUG_LOG,' истина);. If it’s not there, you can insert a new line. Sometimes the line is also commented out and be sure to remove the comments and set the WP_DEBUG_LOG option to true as shown in the image below.
Save the file and upload back to your server. Now reload the site in your browser. WordPress will create a log file called debug.log in the wp-content directory that will log all PHP errors on your site.
This can come in handy, especially when you’re working in a real environment and don’t want users to see errors on their screens. You can then test and fix these bugs behind the scenes without affecting real users.
While the above method is simple, you need to access the server from your hosting account or remotely using FTP to edit wp-config.php. Luckily, you can debug PHP errors in WordPress using various types of plugins. Here we will discuss the following two plugins:
The Debug WP plugin is a free WordPress plugin that launches WordPress debug mode and allows you to log errors. This plugin sets debugging constants in wp-config.php when activated and removes them when deactivated. When problems occur, a PHP exception is thrown. These constants include:
define( 'WP_DEBUG'; true ); define( 'WP_DEBUG_LOG'; true ); define( 'SCRIPT_DEBUG'; true ); define( 'SAVEQUERIES'; true );
When a plugin is deactivated, every effort is made to restore the state of existing constants. However, the default settings and all saved settings are restored when the plugin is reactivated. The plugin also has a debug section where you can set up debug constants. To start;
- Go to your WordPress dashboard and click on the Tools menu.
- Click the “WP Debugging” submenu.
- Scroll down and select the debugging constants you want to apply to your site.
- Click the Save Changes button to apply the changes as shown in the image below.
Debugging PHP Errors with the Query Monitor Plugin
Query Monitor is a free WordPress plugin that helps web administrators analyze web page and web page requests. It will also alert you to serious PHP errors among its many uses. You will see the new item in the top admin panel after installing and activating it. When it identifies a critical (PHP) issue, the toolbar turns red, as shown in the image below.
This dashboard contains a drop-down menu with several options that you can use to manage your WordPress site. However, in this post, we will only focus on PHP errors. Hover over the newly appeared “Query Monitor” toolbar and click “PHP Errors”. This will open a window with all the PHP errors on your WordPress site, as shown in the image below.
In addition, you can set an authentication cookie that allows you to view Query Monitor output when you are not logged in or when you are logged in as a different user. To do this, follow these steps:
- Go to the Plugins section of your WordPress dashboard.
- Click on the “Settings” option under the Query Monitor plugin.
- A developer console window will appear on your admin panel.
- Click “Set Authentication Cookie”.
This post introduces various methods that you can use to debug PHP errors on your WordPress website. If you are good at programming, you should definitely try the manual way of interacting directly with your WordPress code. However, be careful when editing files and don’t create more errors. If you want a quick and easy approach, plugins will come in very handy.