Handy WordPress Debugging and Logging in Development

This handy WordPress debugging and logging snippet during development will help no end when developing themes or plugins, it is always a good idea to have these switched on in the WordPress wp-config.php file.

Replace in wp-config.php

define('WP_DEBUG', false);

with

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

Then for helpful logging of values and arrays to the debug.log file add this snippet to the wp-config.php below the previous code:

if(!function_exists('_log')){
  function _log( $message ) {
    if( WP_DEBUG === true ){
      if( is_array( $message ) || is_object( $message ) ){
        error_log( print_r( $message, true ) );
      } else {
        error_log( $message );
      }
    }
  }
}

Then whenever you need to log anything use

_log( $somevalue );

Source – http://fuelyourcoding.com/simple-debugging-with-wordpress/

About

London based web developer. Creator of Instagrate Pro. Loves WordPress and ribs.

Leave a Reply

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