• Resolved 99w

    (@ninetyninew)


    Hey there,

    We have some code which exports a CSV, this is done using an array of the CSV data:

    
    ob_clean();
    header('Content-type: application/csv');
    
    $fopen1 = 'php';
    $fopen2 = '://';
    $fopen3 = 'output';
    
    $file = fopen( $fopen1 . $fopen2 . $fopen3, 'w' );
    
    foreach ( $csv_rows as $line ) {
    
    	fputcsv( $file, $line );
    
    }
    
    fclose( $file );
    
    exit;
    
    

    When Query Monitor is enabled the bottom of the CSV downloaded includes all of Query Monitor’s HTML output starting with <!– Begin Query Monitor output –>

    Is there a way to disable this for this particular scenario?

    • This topic was modified 4 years, 7 months ago by 99w.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    This depends on what the request looks like when you export the CSV.

    There’s a QM_DISABLED constant that you can define as true to disable QM, but you have to do it very early on in the request, before QM itself loads. You can define it in a mu-plugin or in your wp-config.php if you have a way to identify the request for the CSV export that early on (eg. by looking for a $_GET or $_POST variable). Example:

    if ( isset( $_GET['export_csv'] ) ) {
        define( 'QM_DISABLED', true );
    }

    Similarly, you can filter the view_query_monitor user capability and remove it for the user during that request, but you’ll need to be familiar with the user roles and capabilities system in WordPress to do that.

    There’s nothing else in QM that allows you to conditionally hide its output, but this is something that gets requested occasionally so I hope to add something that enables this in the future.

    Thread Starter 99w

    (@ninetyninew)

    Hey there, thanks – the above is not going to feasible for us in our scenario and how it’s coded currently, for now I’m going to add in a check for Query Monitor being active and if it is display a notice to disable it while exporting. But yeah something in future to disable the output conditionally would be great.

    Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    It just occurred to me that there is in fact a filter in QM for this, I guess I forgot I added it.

    If you add a filter to qm/dispatch/html and return false this will prevent QM from outputting its HTML.

    Note that this doesn’t prevent QM from collecting and processing the data, so if your CSV generation performs a large number of queries then you might see out of memory issues.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Disable Query Monitor output from CSV export’ is closed to new replies.