首页 > 学习笔记 > 一段wordpress的Theme DEBUG程序

一段wordpress的Theme DEBUG程序

作者:yinheli http://philna.com
出处:philna2主题
位置:app/debug.php
作用:开启自定义DEBUG后,在页脚上集中显示错误提示,而不是在分散在出错位置上直接显示
使用方法:在functions.php文件中设置自定义DEBUG选项,define('THEME_DEBUG',true); 或者false.

PHP语言: 高亮代码由发芽网提供
<?php
if(defined('THEME_DEBUG') && constant('THEME_DEBUG')){
//如果定义了自定义DEBUG,则设置错误处理函数
    set_error_handler('ErrorHandler', E_ALL);
    $GLOBALS['PHPErrorMessage'] = array();
    //设置错误信息数组
}
/**
* @param int $errno contains the level of the error raised, as an integer.
* @param string $errstr contains the error message, as a string.
* @param string $errfile which contains the filename that the error was raised in, as a string.
* @param int $errline contains the line number the error was raised at, as an integer.
* @param array $errcontext which is an array that points to the active symbol table at the point the error occurred...
* @return unknown_type
*/
function ErrorHandler($errno, $errstr, $errfile, $errline, $errcontext){
    static $id = 1;
    if(!is_user_logged_in()){
        return;
    //如果不是管理员登录,则直接返回
    }
    //判断错误级别
    switch($errno){
        case E_WARNING : case E_USER_WARNING :
            $type = 'Warning';
            break;
        case E_NOTICE : case E_USER_NOTICE :
            $type = 'Notice';
            break;
        default :
            $type = 'Error';
            break;
    }
    //填充错误信息数组
    $GLOBALS['PHPErrorMessage'][] = 'ID: '.$id.' '.$type.': '.$errfile.' line: '.$errline.' '.$errstr;
    $id++;
    return;
}
//echo E_NOTICE;
function DisplayPHPErrorMessage(){
    //if(is_bot()) return;
    if(isset($GLOBALS['PHPErrorMessage']) && $GLOBALS['PHPErrorMessage']){
        echo '<div style="margin: 0 auto; width: 898px"><h3>PHP errors on this blog</h3><ul>';
        foreach($GLOBALS['PHPErrorMessage'] as $message){
            echo '<li>', $message, '</li>';
        }
        echo '</ul></div>';
    }
}
add_action('wp_footer', 'DisplayPHPErrorMessage', 0);
?>
  1.   帅哥
    邓肯 十一 30th, 2011 @ 18:35 | #1
    沙发! 回复 引用

    错误集中比较好 而且还是在页脚 分散的话就太影响视觉了

  2. 无冷 十二 1st, 2011 @ 10:16 | #2
    藤椅 回复 引用

    都没有找错误的习惯

  3. 秦天SEO 十二 2nd, 2011 @ 13:12 | #3
    板凳 回复 引用

    这个会提示哪里出现错误?是php里面的错误,还是包含js错误的所有错误?

  4.  Admin
    Lwent 十二 2nd, 2011 @ 16:41 | #4
    地板 回复 引用

    @秦天SEO
    提示的是PHP中的错误.js中的错误肯定不会提示啊 不在一起的.
    PHP的错误是在服务器上运行时出现的.js中的错误是在浏览器中运行的时候出现的.不搭边

  5. 乱了感觉 十二 3rd, 2011 @ 11:59 | #5

    本地测试有XDebug辅助了。。。

  6. 石家庄儿童摄影 十二 12th, 2011 @ 11:09 | #6

    表示看不太懂

  7. 雪纺连衣裙 十二 13th, 2011 @ 09:17 | #7

    真的非常不知道怎么用呀

评论提交中, 请稍候...

留言

0
NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks

无觅相关文章插件,快速提升流量