WEB HOSTING

WordPress : How to enbale debug mode ?

WordPress offers an effective debug mode to identify and resolve issues on your site. By enabling it, you can detect errors, warnings, and PHP notices, making it easier to troubleshoot and optimize your web platform. This mode is essential for maintaining a high-performing and secure WordPress site.

Debugging allows you to spot not only apparent malfunctions but also latent issues that could affect your site in the long term. With the built-in tools, you can quickly identify the source of errors, whether they come from the core of WordPress, themes, or plugins.

This article will guide you through activating debug mode and creating a log file, all without disrupting your live site. We will also discuss techniques for customizing the process to meet your needs. While powerful, debugging should be used with caution, especially on production sites.

Whether you are a beginner or an expert, this guide will help you master debugging your WordPress site. Let’s explore the steps to effectively activate and configure this essential mode.

What is debugging?

Debugging in WordPress involves identifying and fixing errors on a site or plugin by analyzing the code and error logs. It covers issues such as broken links, failing plugins, and PHP errors. Once identified, the problems can be resolved by modifying the code, disabling plugins, or applying updates. It is essential to thoroughly test the site after making corrections to ensure it remains stable.

Note: Back up your site before enabling debugging. This way, you can easily restore it if any issues arise. Check out our guide on WordPress backups.

How to enable WordPress debugging?

Log in to your cPanel.
Open the file manager.

Navigate to the root directory of your WordPress installation.

Find and edit the wp-config.php file.

Activate WP_DEBUG

Open wp-config.php

Look for the line: define('WP_DEBUG', false);
If it doesn’t exist, add it just before define(/* That's all, stop editing! Happy publishing. */);

Modify it as follows:

define(‘WP_DEBUG’, true);

This modification activates the general debugging mode of WordPress.

Enable WP_DEBUG_LOG

In the same wp-config.php file Add the following line just after WP_DEBUG:

define(‘WP_DEBUG_LOG’, true);

This will log all error and warning messages to a debug.log file in the wp-content directory.

Enable SCRIPT_DEBUG

Still in wp-config.php Add this line:

define(‘SCRIPT_DEBUG’, true);

This forces WordPress to use the non-minified versions of the core CSS and JavaScript files, which can help debug script-related issues.

Configure a custom PHP debugging

Create a custom plugin or use an existing code snippets plugin.

Add the following function in your plugin or snippet:

php
function write_log($data) { if (true === WP_DEBUG) { if (is_array($data) || is_object($data)) { error_log(print_r($data, true)); } else { error_log($data); } } }

Ensure that WP_DEBUG is enabled in your wp-config.php file:

php
define('WP_DEBUG', true);;

To use this debugging function, you can now call it in your code as follows:

php
write_log(‘DEBUG TEXT’); write_log($variable);

This approach offers several advantages:

  1. It allows for proper formatting of arrays and objects, making them more readable in the logs.
  2. It depends on WP_DEBUG, meaning logging will only occur when debugging mode is enabled.
  3. It provides a consistent and easy-to-use method for logging across your site.

Important notes:

  • Do not add this code directly into your theme’s functions.php file. Using a plugin or snippet manager is preferable for maintenance, security, performance, and code organization.
  • Make sure to disable or remove this code in production, or at least disable WP_DEBUG.
  • Regularly check your log files to prevent them from becoming too large.

This method allows you to log debugging information in a more flexible and readable way while maintaining good WordPress development practices.

Additional Tips

  • It is recommended to disable error display on the production site. To do this, add:

define(‘WP_DEBUG_DISPLAY’, false);

  • After debugging, remember to disable these options by setting them back to false or by removing them, especially on a production site.
  • Regularly check the log files to prevent them from becoming too large.

 

Articles Liés