Skip to content

Backgrounds

How-to Guides

Technical References

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. Query Monitor is available by default on all VIP environments through the VIP Go Mu-Plugins.

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

Enable with the VIP-CLI

Using the VIP-CLI to issue WP-CLI commands, the view_query_monitor capability can be applied to a role or individual users.

Enable for specific roles

Using the wp cap add command, this example will add the view_query_monitor capability for all users with the role “editor”.

vip @wpvip.production -- wp cap add editor view_query_monitor

Enable for specific users

Using the WP-CLI command wp user add-cap to add the capability on a per-user basis, this example will add the view_query_monitor capability for the user login “my_username”.

vip @wpvip.production -- wp user add-cap my_username view_query_monitor

Enable through code

To enable Query Monitor through code, hook into the wpcom_vip_qm_enable filter, found in the query-monitor.php file.

<?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', 'administrator' );
	// 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.

Using 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. Start as a user with the view_query_monitor capability, then use the cogwheel button on Query Monitor’s console and click the “Set authentication cookie” button. Log out or switch users and you should see Query Monitor’s console on both the front end and the admin.

Screenshot of the Query Monitor console with an arrow pointing to the cog icon in the top right corner

Last updated: January 18, 2022