Skip to main content
FolksSpeakersPluginsThemesEventsSnippetsShippedCommunityResources
Login Join
Snippet · PHP

Safe Debug Logger

Shared by Darshit Rajyaguru · April 28, 2026

4 views
1 upvote
Back to Snippets

It will be useful for debugging the data/information of WooCommerce, APIs, AJAX, etc.
Before running this code must enable (‘WP_DEBUG’), (‘WP_DEBUG_LOG’).

if ( ! function_exists( 'wpf_log' ) ) {
    function wpf_log( $data, $label = 'DEBUG' ) {

        if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) return;
        if ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG ) return;

        $time = date('Y-m-d H:i:s');

        $output  = "n[$time] ===== {$label} =====n";
        $output .= ( is_array($data) || is_object($data) ) 
            ? print_r($data, true) 
            : $data;
        $output .= "n=========================n";

        error_log( $output );
    }
}