How to Display PHP Errors on Your WordPress Website

Home » Snippets » How to Display PHP Errors on Your WordPress Website
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you in the process of building a website using PHP and stumbled across some uninvited bugs or errors? It can be particularly frustrating when you don’t know what exactly is triggering these errors. Recognizing such challenges, PHP allows you to display errors that can help you troubleshoot and fix the issues. In this article, we will guide you on how to enable the feature to show PHP errors. We will discuss the different methods to facilitate this process and make your debugging tasks easier and more efficient.

					function wpturbo_show_php_errors() {
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
}
add_action('init', 'wpturbo_show_php_errors');
				

The provided code snippet is fairly straightforward and is designed to turn on PHP error reporting in a WordPress environment, which can be particularly useful when debugging.

Initially, a new function called wpturbo_show_php_errors() is defined. Inside this function, we employ a trio of PHP ini_set() functions in order to directly alter the PHP error reporting behavior.

Let’s explore step-by-step what each line is doing:

The first line within the function

ini_set('display_errors', 1);

enables the display of errors on your website. The ‘display_errors’ parameter is a directive for basic PHP settings. When set to 1, or TRUE, it instructs PHP to display errors that occur during script execution on your site’s pages.

The second line

ini_set('display_startup_errors', 1);

is similar to the first one but focuses on the PHP startup sequence. The ‘display_startup_errors’ directive, when set to 1, or TRUE, directs PHP to display errors encountered during the script’s startup sequence.

The third line

error_reporting(E_ALL);

sets the level of error reporting to include all types of errors, warnings, and notices. E_ALL is the parameter that triggers PHP to report and display all errors, warnings and notices including coding standards warnings.

The final line of the code snippet adds the defined function wpturbo_show_php_errors() to the ‘init’ WordPress action hook.

add_action('init', 'wpturbo_show_php_errors');

The ‘init’ hook is one of the many hooks provided by WordPress and is typically used for tasks that need to occur during the initialization of WordPress environment (after WordPress has finished loading but before headers are sent). By attaching our function wpturbo_show_php_errors() to this hook, we are ensuring that the PHP error display settings are adjusted each time WordPress initializes, allowing for any PHP issues to be immediately visible.

Please, bear in mind that having PHP’s error showing on all the time, particularly in a live site, may lead to exposing sensitive information, so it’s recommended to use it wisely, preferably in a development environment.

Register an account to save your snippets or go Pro to get more features.