Log WordPress Database Queries To File

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

Hello, I am Arun Basil Lal. Thank you for reading!

I am a WordPress product developer and creator of Image Attributes Pro. I am passionate about solving problems and travelling the world.

Divi WordPress Theme - My Review

Divi WordPress Theme
Divi is a WordPress theme that web designers do not want you to know. It comes with a drag-and-drop theme builder. You can build beautiful looking unique websites without touching a line of code. Just choose from one of the many pre-made layouts, or pick elements and arrange them any way you like.

Divi is every WordPress developer's wet dream. Surprise your clients with neat responsive websites and have fun building them.

Divi comes from Elegant Themes. If you enjoy building websites, you *need* an Elegant Themes membership. 87 beautiful themes and 5 plugins for the cost of less than a candy-bar each!


Note: I am an avid user of Divi myself and this is a honest review. I wouldn't recommend something that I do not personally find amazing.

Leave a Reply

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

*