WordPress Snippets

Code Snippet

When developing a WordPress site, you may want to turn on the PHP error warnings and notices to ensure that everything is running smoothly before launch.

PHP

Open your site’s wp-config.php document, located in the root of your WordPress install. Look for one of the following lines (either one may appear, but not both).

define('WP_DEBUG', false); // error reporting is off
define('WP_DEBUG', true); // error reporting is on, but may not be displaying

PHP – Turning PHP Errors On

Replace the existing line of code (whichever of the above is currently in the document) with the following two lines of code. This will ensure that error reporting is not only on, but also being displayed at the top of the frontend (what your users will see) of your page.

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);

Reload the page to see any errors being reported. Make the necessary fixes to your site so that the errors go away.

Note

This is normally NOT something you would turn on for your live site, but instead do so on a temporary staging site to see and fix any errors. Then you would turn off the error reporting and push everything to the live site, where you would then make sure that error reporting is in fact not displaying.

PHP – Turning PHP Errors Off

Once you have the PHP errors fixed, you will want to turn off error reporting.

Comment out the two lines of code you placed to turn on error reporting (or the original one line that was in place if you didn’t replace it above), and place the following four lines of code.

This turns off the PHP error reporting.

// define('WP_DEBUG', true);
// define('WP_DEBUG_DISPLAY', true);

ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_DISPLAY', false);

You can now visit your website to confirm that the PHP errors, notices, and warnings have disappeared from your website.

Note

All modifications to a theme or plugin should be made by creating a child theme and placing the changes there. Changes made to the parent theme will be overwritten the next time it updates.


WordPress Notes:

  • All modifications to a theme or plugin should be made by creating a child theme and placing the changes there; changes made to the parent theme will be overwritten the next time it updates

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.