Log WordPress Database Queries To File

[ad_1]

Useful to see what changes are made in the WordPress database by a plugin, theme or other code.

/**
 * Log database queries to the /wp-content/sql.log file.
 * 
 * @link https://wordpress.stackexchange.com/a/144353/90061
 */
add_filter( 'query', function( $query ){

	// Filter out everything that shouldn't be logged.
	if ( 
		stripos( $query, 'SELECT' ) !== FALSE ||
		stripos( $query, 'SHOW' ) !== FALSE ||
		stripos( $query, '_transient_' ) !== FALSE ||
		stripos( $query, 'iap517_yoast_notifications' ) !== FALSE ||
		stripos( $query, "WHERE `option_name` = 'cron'" ) !== FALSE ||
		stripos( $query, 'iap517_actionscheduler' ) !== FALSE ||
		stripos( $query, 'action_scheduler' ) !== FALSE
	) {
		return $query;
	}

	$file =  WP_CONTENT_DIR . '/sql.log'; // Edit this filepath. 
	@file_put_contents( 
		$file, 
		date( 'c' ) . ' - ' . $query . PHP_EOL, 
		FILE_APPEND | LOCK_EX 
	);

    return $query;
}, PHP_INT_MAX );

Add this to functions.php

[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads