最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

debug - How to find line of code causing wordpress deprecation notices

programmeradmin1浏览0评论

My Site gives a few errors about notices and depreciations that I would like to fix. For example:

Got error 'PHP message: PHP Deprecated: add_option was called with an argument that is deprecated since version 2.3.0 with no alternative available. in /home/public_html/wp-includes/functions.php on line 5067

The line of code it referes to is not the cause of the error, it is the Wordpress debug code intercepting the error. How do I find out the actual line causing the error?

My Site gives a few errors about notices and depreciations that I would like to fix. For example:

Got error 'PHP message: PHP Deprecated: add_option was called with an argument that is deprecated since version 2.3.0 with no alternative available. in /home/public_html/wp-includes/functions.php on line 5067

The line of code it referes to is not the cause of the error, it is the Wordpress debug code intercepting the error. How do I find out the actual line causing the error?

Share Improve this question asked Oct 12, 2020 at 14:41 WillWill 3521 gold badge3 silver badges15 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

The reason why the error message refers to a line in a WordPress core file instead of a theme or plugin is because it's within the function itself that the error occurs, not in the place where add_option() is called.

If you want to be able to trace through the code to discover the originating file, you can use an extension like xdebug, which will allow you to generate a stack trace, like this one:

From this we can see that the offending plugin is called example.php, and the function that calls add_option incorrectly is called some_buggy_function().

As far as fixing the error itself, previous versions of WordPress used a third argument to add_option which is now deprecated. Removing that argument or setting it to an empty string '' will remove the warning.

You could simply run a RegEx search in your editor for add_option\(.+,.+,.+\); and check the results manually. Shouldn't be too much. Alternatively you can run or running $ grep -r 'add_option\(.+,.+,.+\);' . from your command line

For future reference and if anyone is wondering about an easy way to do this:

The referenced line in functions.php should lead you to this function:

function wp_trigger_error( $function_name, $message, $error_level = E_USER_NOTICE ) {

    // Bail out if WP_DEBUG is not turned on.
    if ( ! WP_DEBUG ) {
        return;
    }

    /**
     * Fires when the given function triggers a user-level error/warning/notice/deprecation message.
     *
     * Can be used for debug backtracking.
     *
     * @since 6.4.0
     *
     * @param string $function_name The function that was called.
     * @param string $message       A message explaining what has been done incorrectly.
     * @param int    $error_level   The designated error type for this error.
     */
    do_action( 'wp_trigger_error_run', $function_name, $message, $error_level );

    if ( ! empty( $function_name ) ) {
        $message = sprintf( '%s(): %s', $function_name, $message );
    }

    $message = wp_kses(
        $message,
        array(
            'a'      => array( 'href' => true ),
            'br'     => array(),
            'code'   => array(),
            'em'     => array(),
            'strong' => array(),
        ),
        array( 'http', 'https' )
    );

    if ( E_USER_ERROR === $error_level ) {
        throw new WP_Exception( $message );
    }
}

Add this line

debug_backtrace();

right at the very end of the function and it will output the backtrace just after the error message.

If you want it at least a little "pretty" don't add the debug_backtrace(); instead add this:

//saving backtrace to string
$backtrace = (new Exception)->getTraceAsString();
    
//to make the output a little more readable I added some formatting and linebreaks
echo '<p style="margin:5px; font-family: monospace; font-size: 12px; color:#777;">';
echo str_replace('#','</br>#',$backtrace);
echo '</p>';

(Can't use output buffer, because we already are in a buffer.)

I hope this helps someone! Have a great day and happy hunting.

发布评论

评论列表(0)

  1. 暂无评论