Write custom data to WordPress default debug.log file
The debugging WordPress code is so simple and WordPress have a nice debug systems to simplify the process as well as standardize code across the core, plugins and themes.
During theme & plugin development, I feel that the debug.log
file was the most useful tool. It help me to sort out error in easier manner.
additionally, often I feel that I have to confirm the values of my objects, variables etc in my code during development. The echo
& var_dump()
are too useful. But in few situations, they are not enough.
Below you can find a handy function that help us to write our custom data in the WordPress debug.log
file. Put the below code snippet in your theme’s functions.php
file or your custom plugin file.
if (!function_exists('write_log')) {
function write_log ( $log ) {
if ( true === WP_DEBUG ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
}
Then call the function with our variable in our code like below one.
write_log($my_variable);
Check your debug.log
file and if there is value in your variable, you can see that one.
Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!
Recent Posts
- Automating Code Linting with GitHub Actions for WordPress Plugins
- Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
- The Ultimate Guide to Indexing in Database Design
- Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
- A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.