Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.