Skip to content

Enable Query Monitor

Query Monitor is a WordPress plugin that provides a developer tools panel for WordPress. It enables debugging of database queries, PHP errors, warnings and notices, hooks and actions, block editor blocks, enqueued scripts and stylesheets, HTTP API calls, and more.

The listing for the Query Monitor plugin in the WordPress.org Plugins directory provides a more complete feature list and a guide for usage.

Query Monitor is installed on all WordPress environments by VIP MU plugins.

Enable user access with VIP-CLI

Access to Query Monitor is limited to users that have a view_query_monitor capability assigned to them.

Use VIP-CLI to run a WP-CLI command to add the view_query_monitor capability to user roles or to individual users.

Enable access for specific roles

Use the wp cap add WP-CLI command to add a capability on a per-role basis. In this command example the view_query_monitor capability is added for all users with the Editor role.

vip @mytestsite.develop -- wp cap add editor view_query_monitor

Enable access for specific users

Use the wp user add-cap WP-CLI command to add a capability on a per-user basis. In this command example the view_query_monitor capability is added for a user with the username “my_username”.

vip @mytestsite.develop -- wp user add-cap my_username view_query_monitor

Enable user access through code

Access to Query Monitor can be enabled through code by hooking into the wpcom_vip_qm_enable filter. The code should be added to a file in either an application’s plugins directory or the client-mu-plugin directory.

This code example demonstrates how to enable access to Query Monitor for the user roles vip_support and Editor:

<?php

/**
 * Enable Query Monitor for a defined set of roles or users.
 * 
 * @param bool $enable Whether Query Monitor should be enabled.
 */
function vip_custom_enable_query_monitor( $enable ) {

    $allowed_roles = array( 'vip_support', 'editor' );
	// Optional, enable for specific user logins.
    // $allowed_users = array( 'dev_user_login', 'qa_user_login' );

    if ( is_user_logged_in() ) {
        $user  = wp_get_current_user();
        $login = $user->get('user_login');
        $roles = (array) $user->roles;

        // Allow if the user is assigned a role in the $allow_role array.
        if ( count( array_intersect( $roles, $allowed_roles ) ) > 0 ) {
            return true;
        }

        // Allow if the user's login is in the $allowed_users array.
        if ( isset( $allowed_users ) && in_array( $login, $allowed_users ) ) {
            return true;
        }
    }

    return $enable;
}
add_filter( 'wpcom_vip_qm_enable', 'vip_custom_enable_query_monitor' );

Caution

When enabling Query Monitor through code, avoid disabling access for VIP Support users whose roles will appear as vip_support.

If the wpcom_vip_qm_enable filter returns false for users with the role vip_support, VIP team members will not be able to use Query Monitor in a situation where support is required. This can delay VIP’s ability to promptly help especially during urgent or high-priority issues.

Use Query Monitor when logged out

It is possible to view Query Monitor’s debugging output when not logged in, or when logged in as a user without the view_query_monitor capability.

  1. Log in as a user with the view_query_monitor capability.
  2. Select the cogwheel icon in the upper right of Query Monitor’s console.
  3. Select the button labeled “Set authentication cookie“.
  4. Log out or switch users, then refresh the page.

Query Monitor’s console will be visible on both the front end and in the WordPress Admin dashboard.

Screenshot of the Query Monitor console with an arrow pointing to the cog icon in the top right corner
Example screenshot of the view of Query Monitor after selecting the cogwheel icon.

Last updated: January 31, 2024

Relevant to

  • WordPress