Failing to Suppress PHP Deprecated Messages in WordPress with a Workaround

Synopsis

Some Background on my Failure

I originally wrote an article on how to suppress PHP deprecated messages in the WordPress Debug Log, however, shortly after publishing the article, I found that my "fix" was actually failing miserably. Since then, I've been trying every method I could find, old and new, and none seem to get the job done. The closest I've come is being able to hide all PHP messages in the debug log which really does nothing but make me believe there is a way out there that I just haven't uncovered yet.

In the interim, I have accepted my defeat and temporarily ended my side quest. I have instead pivoted and found a workaround which I'll be sharing with you so you can continue your web development without losing your sanity to an endless barrage of PHP deprecated messages.

The Problem

If you're one of the many web developers that has migrated to PHP 8 since the recent retirement of PHP 7.4, you may have noticed a lot of PHP Deprecated messages in the WordPress debug log. Ideally, all of these messages should be addressed, but a lot of times these come from themes or plugins you don't control and while informative, can become quite obstructive when debugging your own code.

Make Your Own Log

The workaround I've found most useful is to output to a custom log file completely separate from the WordPress debug log. You can accomplish this by either create the following function in the functions.php of your WordPress site or within the code of your plugin if that's what you're developing.

if ( !function_exists( 'custom_debug_log' ) ) {
	function custom_debug_log( $log ) {
		// Check if WP Debug is enabled
		if ( WP_DEBUG === true ) {
			$current_date_time = "[" . wp_date('Y-m-d H:i:s T') . "]";

			if ( is_array( $log ) || is_object( $log ) ) {
				error_log( $current_date_time . " " . print_r( $log, true ) . "\n", 3, WP_CONTENT_DIR . "/custom-debug.log" );
			} 
			else {
				error_log( $current_date_time . " " . $log . "\n", 3, WP_CONTENT_DIR . "/custom-debug.log" );
			}
		}
		return;
	}
}

This function will output the current date, time & timezone of your site along with any output you give it. The log file will be created in the same directory as the debug log file, wp-content which you can find in the root directory of your WordPress site. It checks if WP Debug is enabled first and also handles any arrays or objects you need to output.

*NOTE: Keep in mind that if your code generates any PHP deprecated messages, warnings or errors, they will still output to the default WordPress debug.log

How Do I Use It?

Using this function is very simple. Just call it like so anywhere within your PHP code.

custom_debug_log( "Write whatever you want" );

You'll then see a newly created custom-debug.log file within the wp-content directory which should now contain the following line.

[2022-12-29 16:09:33 PST] Write whatever you want

You'll see only the messages you want without all of the errors, warnings, etc. from PHP. You're free to customize this function anyway you want.

For example, you can remove the WP Debug check by removing the if ( WP_DEBUG === true ) statement or change the output directory to anywhere else within your WordPress site.

I'm Not Beat Yet

When my schedule clears up, I'll continue on my quest to suppress deprecated messages in the WordPress debug log or perhaps I'll find someone else who already did. If you have a method that works for you, please comment below and I'll update or create a new post based on it.

Please Share Me, I'm Lonely