WordPress: Debug Mode – Enable & Locate Logs

Do you need to find and fix some errors in a WordPress website?

The WordPress comes with a built-in debug mode that can be enabled in the website’s wp-config.php file.

This note shows how to enable the debug mode in the WordPress and how to find the location of the debug.log file.

Cool Tip: Trigger an action (send an email or a message to Telegram) when a new comment is posted on a WordPress site! Read more →

Enable Debug Mode in WordPress

To enable the debug mode in the WordPress, edit the wp-config.php file in the root directory of your website and set the WP_DEBUG to true.

To also log the errors to a file, set the WP_DEBUG_LOG to true (by default this line is absent).

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);

If you want, you can also, a kind of, “enable WP_DEBUG for admins only” by telling the WordPress and PHP to not display errors on your website, to not disturb the visitors:

define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors',0);

After the WP_DEBUG and WP_DEBUG_LOG are enabled, you will find a file called debug.log in the /wp-content/ folder of your website:

/wp-content/debug.log

Warning: The built-in WordPress debugger is hard-coded to log to the /wp-content/ folder that is accessible from the web, that is probably not good for production!

Debug functions.php

If you need to debug some PHP code in your WordPress child theme’s functions.php file, you can create a log file and append it with the data that you want to analyze, e.g. values of some variables.

Below is an example of the notify_by_email function from my functions.php file with the highlighted debug part:

<?php
  function notify_by_email( $comment_id, $comment_approved ) {
  if ( ! $comment_approved ) {
  $comment = get_comment( $comment_id );
  $mail = 'my_address@email.tld';
  $subject = sprintf( 'New Comment by: %s', $comment->comment_author );
  $message = $comment->comment_content;
  wp_mail( $mail, $subject, $message );
  $fa = fopen($_SERVER['DOCUMENT_ROOT'] . 'functions-php-debug.log', 'a');
  fwrite($fa, "\n##### Print Variables #####\n");
  fwrite($fa, $comment_id);
  fwrite($fa, $message);
  fwrite($fa, "\n##### Print Array #####\n");
  fwrite($fa, print_r($post, true));
  }
  }
  add_action( 'comment_post', 'notify_by_email', 10, 2 );
?>

In this example i am creating the functions-php-debug.log file in the website’s root directory and log there the values of the variables that i want to check.

Happy debugging!

Leave a Reply