write_log( mixed $log, bool $send_to_console = false)
This will write to the error log found in the wp-content folder. If $send_to_console is set to true, it will also try and send it to the console of the browser.
function write_log( $log, $send_to_console = false ) {
if ( true === WP_DEBUG ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
if ( $send_to_console ) {
debug_to_console( $log );
}
}
}
For this to all work you must have some constants defined in the wp-config file which is found in the root folder of your WordPress install. You msut have these defined:
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
Usage
$foo = 'Some Text';
write_log( $foo, true);
You can even write an array. Here is a practical example that can help you see what is being queried by WordPress.
global $wp_query;
write_log( $wp_query->query_vars, true);
Below, you can see the write_log in action as I output the query to the console log.