• Opening this ticket as a follow-up to this ticket that doesn’t really provide a solution but is currently closed.

    Step 1: Set a persistent cookie for whitelisted users or a session cookie via a “qm” querystring argument on the init action hook. This can go in functions.php, plugin / class file, etc.

    add_action( 'init', 'tl_maybe_set_query_monitor_cookie', 1 );
    
    function tl_maybe_set_query_monitor_cookie() {
    
        // this is the whitelist of query monitor users
        // could also put this in a site option, etc
        $qm_users = array( 8, 60, 55232 );
    
        // see if whitelisted user
        if( in_array( get_current_user_id(), $qm_users ) ):
    
            // set a persistent cookie if whitelisted user
            setcookie( 'tl_qm', 1, strtotime( '+1 year' ), '/' );
    
        // see if "qm" querystring
        elseif( isset( $_GET['qm'] ) ):
    
            // set a session cookie if URL has "qm" querystring
            setcookie( 'tl_qm', 1, 0, '/' );
    
        endif;
    
    }

    Step 2: Create an mu-plugin that does a one-time filter on the active_plugins option.

    global $tl_assessed_qm;
    $tl_assessed_qm = false;
    
    add_filter( 'option_active_plugins', 'tl_turn_off_qm', 10, 1 );
    
    function tl_turn_off_qm( $plugins ){
    
        global $tl_assessed_qm ;
    
        if( $tl_assessed_qm ) return $plugins;
    
        $key = array_search( 'query-monitor/query-monitor.php' , $plugins );
    
        if( isset( $_COOKIE['tl_qm'] ) ):
    
            $plugins[] = 'query-monitor/query-monitor.php';
    
        else:
    
            unset( $plugins[$key] );
    
        endif;
    
        $tl_assessed_qm = true;
    
        return $plugins;
    
    };

    The only caveat I’ve found so far is that the very first page load for whitelisted users will not have QM turned on because the logic to prevent the plugin from running happens in an mu-plugin before the cookie gets set.

    Hope this is helpful to someone.

Viewing 1 replies (of 1 total)
  • Thread Starter toddlevy

    (@toddlevy)

    This seemed to have stopped working on my setup so I’ve removed the check to see if it’s already been assessed.

    add_filter( 'option_active_plugins', 'tl_turn_off_qm', 10, 1 );
    
    function tl_turn_off_qm( $plugins ){
    
        $key = array_search( 'query-monitor/query-monitor.php' , $plugins );
    
        if( isset( $_COOKIE['tl_qm'] ) ):
    
            $plugins[] = 'query-monitor/query-monitor.php';
    
        else:
    
            unset( $plugins[$key] );
    
        endif;
    
        return $plugins;
    
    };
Viewing 1 replies (of 1 total)
  • The topic ‘Enabling Query Monitor for only specific users’ is closed to new replies.