Then you could log it into the
sql.log
file. Here's a simple example:/**
Dump all database queries to the /wp-content/sql.log file. */ add_action( 'shutdown', function(){
$file = WP_CONTENT_DIR . '/sql.log'; // Edit this filepath to your needs.
if( current_user_can( 'manage_options' ) && file_exists( $file ) && is_writeable( $file ) && isset( $GLOBALS['wpdb']->queries ) ) file_put_contents( $file, date( 'c' ) . PHP_EOL . print_r( $GLOBALS['wpdb']->queries, TRUE ), FILE_APPEND );
});
or use the query
filter of the wpdb
class to log only the INSERT
, UPDATE
and DELETE
queries:
/**
* Log the INSERT, UPDATE, DELETE database queries to the /wp-content/sql.log file.
*/
add_filter( 'query', function( $query ){
if( FALSE !== stripos( $query, 'UPDATE ' )
|| FALSE !== stripos( $query, 'INSERT ' )
|| FALSE !== stripos( $query, 'DELETE ' )
) {
$file = WP_CONTENT_DIR . '/sql.log'; // Edit this filepath to your needs.
if( file_exists( $file ) && is_writeable( $file ) )
file_put_contents(
$file,
date( 'c' ) . ' - ' . $query . PHP_EOL,
FILE_APPEND | LOCK_EX
);
}
return $query;
}, PHP_INT_MAX );