I have an isue with Pingdom speed test scores

I've ran some of the sites in my multisite through the Pingdom Website Speed Test and they're averaging in the low 70s. The lowest factor in the scores involved "Remove query strings from static resources" which received a score of 41 on one site. I raised that to an 86 by creating a child theme with a functions.php file in it. I tweaked it with the following code-

<?php
/*
* Force URLs in srcset attributes into HTTPS scheme.
* This is particularly useful when you're running a Flexible SSL frontend like Cloudflare
* Forces http images to load on https sites
*/
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}

function ssl_srcset( $sources ) {
foreach ( $sources as &$source ) {
$source['url'] = set_url_scheme( $source['url'], 'https' );
}

return $sources;
}
add_filter( 'wp_calculate_image_srcset', 'ssl_srcset' );

/**
* Remove Query strings from Static Resources.
*/
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
?>

I was also trying to raise a score of of 48 for "Specify a Vary: Accept-Encoding header" in the same functions.php but have not been able to find the correct code to do so.

First question: can someone suggest the proper code to add to functions.php to rase my "Specify a Vary: Accept-Encoding header" score?

Second question: Is there a way to accomplish the same goals at the multisite level without having to add a child theme to each site?

Thanks,
Larry

  • Adam Czajczyk

    Hello Larry,

    I hope you're fine today!

    First question: can someone suggest the proper code to add to functions.php to rase my "Specify a Vary: Accept-Encoding header" score?

    This usually means enabling gzip compression for the site. The first thing you'll want to check (you may want to contact your hosting provider tech staff for this) is to make sure that the server that your site is hosted on does support "gzip compression". If its using Apache web server, the "mod_deflate" module should be enabled.

    Once this is confirmed, you may want to either give our WP Hummingbird plugin a try (it supports gzip compression out of the box) or try adding this lines to your site's ".htaccess" file:

    <IfModule mod_headers.c>
      <FilesMatch ".(js|css|xml|gz|html)$">
        Header append Vary: Accept-Encoding
      </FilesMatch>
    </IfModule>

    Please note however that if you're using a CDN to speed up your site (such as CloudFlare or MaxCDN) then according to some reports from WP users Pingdom tools may cause "false negative" warnings on this, stating that this is still disabled.

    Second question: Is there a way to accomplish the same goals at the multisite level without having to add a child theme to each site?

    This should be possible with so called "Must Use Plugin". To achieve this remove the code from your theme's functions.php file and then create an empty file with a .php extension and paste the code inside like this:

    <?php
    /*
    * Force URLs in srcset attributes into HTTPS scheme.
    * This is particularly useful when you're running a Flexible SSL frontend like Cloudflare
    * Forces http images to load on https sites
    */
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    
    }
    
    function ssl_srcset( $sources ) {
    foreach ( $sources as &$source ) {
    $source['url'] = set_url_scheme( $source['url'], 'https' );
    }
    
    return $sources;
    }
    add_filter( 'wp_calculate_image_srcset', 'ssl_srcset' );
    
    /**
    * Remove Query strings from Static Resources.
    */
    function remove_cssjs_ver( $src ) {
    if( strpos( $src, '?ver=' ) )
    $src = remove_query_arg( 'ver', $src );
    return $src;
    }
    add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
    add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

    The file should be then uploaded to your server via FTP, to the "/wp-content/mu-plugins" folder. If there's no "mu-plugins" folder inside "/wp-content" plugin just create it.

    Any "MU plugin" applied that way would affect all sites of Multisite install.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.