Skip to content

WordPress – Log all queries for debugging

Simple way to log all queries in wordpress: Add the below code in your functions.php

    add_action('shutdown', 'sql_logger');
    function sql_logger()
    {
        global $wpdb;
        $log_file = fopen(ABSPATH . 'sql_log.txt', 'a');
        fwrite($log_file, "//////////////////////////////////////////\n\n" . date("F j, Y, g:i:s a") . "\n");
        foreach ($wpdb->queries as $q) {
            fwrite($log_file, $q[0] . " - ($q[1] s)" . "\n\n");
        }
        fclose($log_file);
    }
Published inCodingWordpress

Be First to Comment

Leave a Reply

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