This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

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

Iain Poulson is a WordPress and PHP developer, writer, and plugin author. His plugins include Intagrate the WordPress publishing for Instagram, and WP User Manager the membership, user profile, and community plugin. He also co-hosts the WordPress development and business podcast Pressing Matters. You can find him on Twitter @polevaultweb.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.