I am currently working on this plugin, which is similar to Query Monitor in which i Have displayed all the Details present in the $wpdb object. Now i need to find which plugin is Triggering the Query.

Code is Here :-

global $wpdb;

$data = $wpdb->queries;
$query_time = 0;

$total_time = round(timer_stop(false, 22 ),4);
if ($wpdb->queries != null)
    foreach ( $wpdb->queries as $query )
        $query_time = $query_time + $query[1];

$query_time = round($query_time,4);

echo "<table>";
    echo "<tr><td>".$wpdb->num_queries." Queries in ".$query_time." Seconds. Page Loaded in ".$total_time." Seconds.</td></tr>";
echo "</table>";

echo "<table>";
echo "<tr> <td>S.No</td><td>Query</td><td>Components</td><td>Rows</td><td>Time Taken</td> </tr>";
foreach($data as $data_key => $data_value)

    $rowcount = sizeof($wpdb->get_results($data_value[0]));
    $query = $data_value[0];
    $time_taken = round($data_value[1],4);
    $components = explode(",",$data_value[2]);

echo "<tr>";
    echo "<td>".($data_key+1)."</td>";
    echo "<td>".$query."</td>";
    echo "<td>";
        foreach($components as $component_value)
            //if(strpos($component_value, 'require') === false && strpos($component_value, 'include') === false)
                echo $component_value."<br>";
    echo "</td>";
    echo "<td>".$rowcount."</td>";      
    echo "<td>".$time_taken."</td>";
echo "</tr>";
echo "</table>";
  • 2
    That information isn't tracked, you're going to need to track it yourself by inserting hooks. Query Monitor is a good example of how to do this – Tom J Nowell Aug 1 '18 at 12:44
  • I am new to plugin development. can you please elaborate because i am looking into the Query Monitor Code for 7 days now and i am unable to go further.... It will be helpful – Akash Manhas Aug 1 '18 at 17:22
  • 1
    You would have to look at the Query Monitor code for the answer, or ask the Query Monitor developers how they did it. It's extremely uncommon/rare/unusual for code to do what you're asking, and there is no API for it. Especially since plugins are not objects, once code is loaded, it's just code without context, queries don't get tagged with the plugin that called them, the information simply isn't saved. If you want that information you're going to have to collect it yourself and build the functionality from scratch. What you ask is not trivial easy or simple – Tom J Nowell Aug 1 '18 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.