Skip to content

How to debug PHP errors on a WordPress site?

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 in WordPress

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.

  1. Debug PHP errors manually with code.
  2. Debug PHP errors with plugins.
See also  How to remove query strings in WordPress?

1. Debug PHP errors manually with code

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.

We will look at three basic PHP constants:

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

WP_DEBUG and WP_DEBUG_DISPLAY

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.

Error is not displayed
Error is not displayed

To set WordPress debugging to true, navigate to your site’s root installation folder and locate the wp-config.php file. Edit the file and check if it contains the following line.

define( 'WP_DEBUG', false );

It should look like the image below.

Enable Debugging in WordPress
Enable Debugging in WordPress

If the line is there, just change the value of WP_DEBUG to true. Otherwise, you can insert a new row with WP_DEBUG set to true. Also, add the line below to enable PHP error display in the browser.

define( 'WP_DEBUG_DISPLAY', true );

With both options enabled, it should look like the image below.

Enable error display
Enable error display

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.

Displaying PHP site errors in the browser
Displaying PHP site errors in the browser

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.

See also  How to replace a dead CMOS battery

But first, don’t forget to disable error display by editing the value of WP_DEBUG_DISPLAY as shown in the image below. Leave WP_DEBUG set to true so we can log errors.

Disable error display
Disable error display

Error Log with WP_DEBUG_LOG

The following are some of the situations in which you might want to create a WordPress error log file:

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.

Enable debug error log
Enable debug error log

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.

Viewing the debug error log
Viewing the debug error log

When we open the file, you can see that it contains all the errors that were displayed on our site.

Site error logs
Site error logs

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.

2. Debugging PHP errors with plugins

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:

  1. WP Debugging Plugin
  2. Query Monitor Plugin

Debugging PHP Errors with the WP Debugging Plugin

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.
WP Debugging Plugin Settings
WP Debugging Plugin Settings

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.

Query Monitor Top Panel
Query Monitor Top Panel

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.

Query Monitor Errors
Query Monitor Errors

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”.
Set authentication cookie
Set authentication cookie

Conclusion

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.

See also  How to remove query strings in WordPress?

Leave a Reply

Your email address will not be published. Required fields are marked *