当前位置: 首页 > news >正文

[改变自己wordpress.2]至wordpress再加上简单的debug sql调试.

或者说,同事. 需要帮助她打印出来sql 调试输出到页面sql


在这里,我们使用插件或一个的方式来启动配置文件wordpress的debug


在插件文件夹 wordpress/wp-content/plugins/ 新建一个文件叫bt_debug_sql.php 

<?php
/*
Plugin Name:    Bt Debug Sql.
Plugin URI:     http://www.btroot.com
Description:    Show the sql at the frontpage.
Author:         rainysia
Version:        1.0
Author URI:     http://www.btroot.org
 */


/**
 * Show debug Sql.
 *
 * @return void
 */
add_action('init', 'bt_debug_sql');


function bt_debug_sql() {
    if (!defined('SAVEQUERIES')) {
        define('SAVEQUERIES', true);
    }
    if (has_action('wp_footer')) {
        add_action('wp_footer', 'snx_show_sql');
    }
}
// show sql
function snx_show_sql(){
    if (isset($_GET['debug']) && $_GET['debug'] == true) {
        global $wpdb;
        //echo '<pre>'; 
        //print_r ($wpdb->queries);
        $all_debug_queries = $wpdb->queries;
        echo "<table>";
        if (isset($all_debug_queries) && count($all_debug_queries) > 0) {
            $total_time_cost = 0;
            $large_time_cost = 0;
            $large_sql_query = '';
            $large_sql_num = 0;
            $debug_file = false;
            if (isset($_GET['debug_file']) && $_GET['debug_file'] == true) {
                $debug_file = true;
            }
            echo '<tr style="font-weight:bold;"><td>Num</td><td>Times</td><td>SQL</td>';
            echo $debug_file ? '<td>Include files</td><tr>' : '';
            foreach ($all_debug_queries as $k => $v) {
                // $v[0] is sql, $v[1] is time, $v[2] is relative file.
                $total_time_cost += $v[1];
                if ($large_time_cost <= $v[1]) {
                    $large_time_cost = $v[1];
                    $large_sql_query = $v[0];
                    $large_sql_num = $k + 1;
                }
                if (($k + 1) % 2 == 1) {
                    echo '<tr style="background-color:#8F9196;color:rgb(202, 227, 253);"><td style="font-weight:bold;text-align:center;">'.($k + 1).'</td>';
                } else {
                    echo '<tr><td style="font-weight:bold;text-align:center;">'.($k + 1).'</td>';
                }
                echo '<td style="text-align:left;">'.sprintf("%.10f", $v[1]).'</td>';
                echo '<td style="padding-left:6px;text-align:left;">'.$v[0].'</td>';
                echo $debug_file ? '<td style="text-align:left;">'.$v[2].'</td>' : '';
                echo '</tr>';
            }
        }
        echo '<div style="text-align:left;">';
        echo '<div style="widht:100%;height:3px;margin:10px 0;background:rgba(56, 83, 129, 0.85)!important; -moz-box-shadow:0px 0px 1px #c3c3c3; -webkit-box-shadow:0px 0px 1px #c3c3c3; -box-shadow:0px 0px 1px #c3c3c3;"></div>';
        echo '<div>The whole sql queries cost:<span style="color:red;"> '.$total_time_cost.'s</span></div>';
        echo 'The large sql is No.<span style="color:red;">'.$large_sql_num.'</span>  cost: <span style="color:red;">'.$large_time_cost.'s</span><br />  <span style="color:rgb(194, 76, 95);background-color:rgb(208, 210, 218);">'.$large_sql_query.'</span><br />';
        echo '<div style="widht:90%;height:3px;margin:10px 0px;background:rgba(128, 143, 158, 0.85)!important; -moz-box-shadow:0px 0px 1px #c3c3c3; -webkit-box-shadow:0px 0px 1px #c3c3c3; -box-shadow:0px 0px 1px #c3c3c3;"></div>';
        echo "</table>";
        echo '</div>';
    }
}



这里的作用仅仅是为了让wordpress的SAVEQUERIES 静态变量设为true, 所以你也能够不这样, 直接在wp-config.php 配置文件中面加一句

define('SAVEQUERIES', true);
效果是一样.

接着在/wp-content/theme/你的主题/footer.php 最后一个</div> 前加上以下的代码来调用一次wp_footer();保存就可以.

<?php wp_footer();?

>




保存后就能够了. 然后去启用插件, 使用的时候, 在当前页面的url后跟?

debug=true 能够开启debug sql模式, 加debug_file=true会显示include的关联文件.

http://test.com/solutions/vwideformat/test/?debug=1 和

http://test.com/solutions/vwideformat/test/?debug=1&debug_file=1

的影响,如以下




转载于:https://www.cnblogs.com/hrhguanli/p/4584141.html

相关文章:

  • 鼠标移上去下拉框出现移走或者点击之后 下拉框消失的效果。
  • Android 2.3系统令人为之兴奋的十大理由
  • 简单模拟MouseEvent.ROLL_OUT MouseEvent.ROLL_OVER MouseEvent.MOUSE_MOVE
  • 一个博士学姐的心路历程和肺腑之言
  • 遇到问题:如果 AVM1 内容(AS1 或 AS2)已加载到 AVM2 (AS3) 内容中,则不可将其移动到 displayList 的其他部分...
  • Linux gperf命令
  • main函数之前究竟发生了什么?
  • GET和POST的区别(详解)
  • 不可或缺的PrepareImageRegions函数
  • ExternalInterface.addCallback(functionName:String, closure:Function):void
  • store加载时传递动态参数
  • C++ VS C#(3):switch,类型转换
  • 课程改进意见
  • 《大道至简》3
  • 项目管理学习笔记六:项目范围管理
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • HomeBrew常规使用教程
  • HTTP中GET与POST的区别 99%的错误认识
  • IDEA 插件开发入门教程
  • js如何打印object对象
  • Kibana配置logstash,报表一体化
  • Linux中的硬链接与软链接
  • SegmentFault 2015 Top Rank
  • socket.io+express实现聊天室的思考(三)
  • Swoft 源码剖析 - 代码自动更新机制
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 翻译:Hystrix - How To Use
  • 记一次删除Git记录中的大文件的过程
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 聊聊flink的BlobWriter
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 你不可错过的前端面试题(一)
  • 数组的操作
  • 小而合理的前端理论:rscss和rsjs
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 正则表达式小结
  • 《码出高效》学习笔记与书中错误记录
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 从如何停掉 Promise 链说起
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ​马来语翻译中文去哪比较好?
  • ​批处理文件中的errorlevel用法
  • #1014 : Trie树
  • #stm32驱动外设模块总结w5500模块
  • (07)Hive——窗口函数详解
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (8)STL算法之替换
  • (AngularJS)Angular 控制器之间通信初探
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (黑马C++)L06 重载与继承
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解