[DIY change wordpress.2] to wordpress plus simple debug SQL debugging.

Source: Internet
Author: User
Tags wordpress debug

or that colleague. This time, I need to help her print out SQL output to the page to debug SQL


Here we use plug-ins or a configuration file to start the WordPress debug


Create a new file in the plugin directory wordpress/wp-content/plugins/called bt_debug_sql.php

<?php/*plugin Name:   &NBSP;BT Debug sql.plugin URI:     http://www.btroot.comDescription:   &NBSP ; Show the SQL at the FrontPage. Author:         rainysiaversion:        1.0author URI:     HTTP://WWW.BT root.org *//** * Show Debug sql. * * @return void */add_action (' init ', ' bt_debug_sql '); function Bt_debug_sql () {    if (!defined (' savequeries ')) {        define (' Savequeries ', tr UE);   }    if (has_action (' Wp_footer ')) {        add_action (' Wp_footer ', ' snx_s How_sql ');   }}//show SqlFunction snx_show_sql () {    if (isset ($_get[' Debug ') && $_get[' Debug '] = = true) {        Global $wpdb;       //echo ' <pre> ';   & nbsp    //print_r ($wpdb->queries);        $all _debug_queries = $wpdb->queries;  & nbsp    Echo <table>;        if (Isset ($all _debug_queries) && count ($all _DEBUG _queries) > 0) {            $total _time_cost = 0;          &NBSP ; $large _time_cost = 0;            $large _sql_query = ';          &N Bsp  $large _sql_num = 0;            $debug _file = false;            if (Isset ($_get[' debug_file ')) && $_get[' debug_file '] = = true) {                $debug _file = true;           }            Echo ' <t R 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]) {          &NB Sp          $large _time_cost = $v [1];                    $large _sql_query = $v [0];                    $large _sql_num = $k + 1;&N Bsp               }                if ($k + 1)% 2 = = 1) {                    echo ' <tr style= ' Background-color: #8F9196 ; Color:rgb (202, 227, 253); " &GT;&LT;TD style= "Font-weight:bold;text-align:center;" > '. ($k + 1). ' </td> ';               } else {                    Echo ' <tr&gt ; <td style= "font-weight:bold;text-align:center;" > '. ($k + 1). ' </td> ';               }              &NB Sp 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> ';        & nbsp  }       }        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, (+); Background-color: RGB (208, 210, 218); " > '. $large _sql_query. ' </span><br/> ';        echo ' <div style= ' widht:90%;height:3px;margin:10px 0px; Background:rgba (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> ';   }} 

The function here is just to let WordPress savequeries static variable set to True, so you can also not, directly in the wp-config.php configuration file inside add a sentence

Define (' Savequeries ', true);
The effect is the same.

Then in/wp-content/theme/your subject/footer.php the last </div> before adding the following code to call once Wp_footer (); Save it.

<?php wp_footer ();? >




After you save it. Then go to enable the plugin, when used, at the current page of the URL followed? debug=true can open debug SQL mode, plus debug_file=true will display the associated file of the include.

Http://test.com/solutions/vwideformat/test/?debug=1 and

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

The effect is as follows




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.