Log Deprecated and "Doing It Wrong" Notices With Their Source
Writes every deprecated function, argument, hook or "doing it wrong" notice to the PHP error log with the plugin or theme file that caused it, even when WP_DEBUG is off.
Opens WPIDE on
Code
$wpide_log_notice = function ( $kind, $message ) {
static $seen = array();
$key = md5( $kind . $message );
if ( isset( $seen[ $key ] ) ) {
return;
}
$seen[ $key ] = true;
// Find the first frame outside WordPress core.
$source = '';
foreach ( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 25 ) as $frame ) {
if ( empty( $frame['file'] ) || false !== strpos( $frame['file'], ABSPATH . WPINC ) || false !== strpos( $frame['file'], ABSPATH . 'wp-admin' ) ) {
continue;
}
$source = str_replace( ABSPATH, '', $frame['file'] ) . ':' . ( $frame['line'] ?? 0 );
break;
}
error_log( sprintf( '[WP %s] %s%s', $kind, $message, $source ? ' (from ' . $source . ')' : '' ) );
};
add_action( 'deprecated_function_run', function ( $function, $replacement, $version ) use ( $wpide_log_notice ) {
$wpide_log_notice( 'deprecated function', "$function() since $version" . ( $replacement ? ", use $replacement" : '' ) );
}, 10, 3 );
add_action( 'deprecated_argument_run', function ( $function, $message, $version ) use ( $wpide_log_notice ) {
$wpide_log_notice( 'deprecated argument', "$function() since $version: $message" );
}, 10, 3 );
add_action( 'deprecated_hook_run', function ( $hook, $replacement, $version ) use ( $wpide_log_notice ) {
$wpide_log_notice( 'deprecated hook', "$hook since $version" . ( $replacement ? ", use $replacement" : '' ) );
}, 10, 3 );
add_action( 'doing_it_wrong_run', function ( $function, $message, $version ) use ( $wpide_log_notice ) {
$wpide_log_notice( 'doing it wrong', "$function(): " . wp_strip_all_tags( $message ) );
}, 10, 3 );
Notes
Turn it on before upgrading WordPress or PHP, then check the error log; each distinct message is logged once per request.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.