0

I have an issue similar to many others in the web, but still with no solution so far.

To remove query strings i've used the following code:

function _remove_script_version( $src ){
    $parts = explode( '?', $src );
    return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );

What i'ld like, is to know if someone believe to know how to set up a code to exclude googple map api query string from being removed.

To fix the error below:

“The Google Maps API server rejected your request. The “sensor” parameter specified in the request must be set to either “true” or “false”.

Does anyone beleive to know how to fix this?

1

First of all I would like to strongly recommend you to not remove query parts of javascript and stylesheet files. Why? Because they contain a version of a file.

Why do I need it for? We need to add version to each file (whenever it is possible) to prevent versions collisions. These collisions could appear when a new version of WP/plugin is released and you update it from admin panel. In this case, your users' browser won't be notified that a new version of a script exists and won't download it, but will use previous one which is stored in browser cache.

But if you strongly confident that you don't need it and still want to trim query strings, you can do it by changing your function to the following view:

function _remove_script_version( $src ){
    return add_query_arg( 'ver', false, $src );
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
  • Thanks for your extraordinary fast reply. I've tried to insert that code, but had no success, the query string is still being removed. Meanwhile, my goal is to acomplish pingdom recomendations in order to get the best performance possible. – Bdalte Sep 15 '13 at 21:46
  • I have updated my snippet, it will remove all ver param only and leave else params including sensor one. Check it out! – Eugene Manuilov Sep 15 '13 at 21:57
  • I found this approach in web: function hide_wp_version($src) { global $wp_version; return str_replace("?ver=$wp_version", '', $src); } add_filter('script_loader_src', 'hide_wp_version'); add_filter('style_loader_src', 'hide_wp_version'); Does it makes any sense? – Bdalte Sep 15 '13 at 22:04
  • My snippet is shorten then what you've found and does the same thing. Just try to use it and it has to work for you. – Eugene Manuilov Sep 15 '13 at 22:06
  • Very nice. You win. :) – s_ha_dum Sep 15 '13 at 22:06
0

This would be my approach:

function _remove_script_version( $src ){
  $parsed = parse_url($src);

  if (isset($parsed['query'])) {
    parse_str($parsed['query'], $qrystr);
    if (isset($qrystr['ver'])) {
      unset($qrystr['ver']); 
    }
    $parsed['query'] = http_build_query($qrystr);
  }
  // return http_build_url($parsed); // elegant but not always available

  $src = '';
  $src .= (!empty($parsed['scheme'])) ? $parsed['scheme'].'://' : '';
  $src .= (!empty($parsed['host'])) ? $parsed['host'] : '';
  $src .= (!empty($parsed['path'])) ? $parsed['path'] : '';
  $src .= (!empty($parsed['query'])) ? '?'.$parsed['query'] : '';

  return $src;
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );

You would explicitly unset particular query parameters, in this case only the ver parameter, leaving everything else intact.

As far as query strings and caching: https://stackoverflow.com/questions/850187/caching-from-urls-with-a-query-string

  • I guess that would be a very good approach, but don't know why, a lot of code appeared on top of wp dashboard... – Bdalte Sep 15 '13 at 21:58
  • 1
    oops... I left in a var_dump. :) Fixed. – s_ha_dum Sep 15 '13 at 21:58
  • @user2607556 : see the edit. – s_ha_dum Sep 15 '13 at 22:04
  • I've tested... and it rocks! Gave my pingdom query string score, an amazing 22!! It's the best score i ever had regarding query strings removed from static resources... Is there any other way to boost this score? – Bdalte Sep 15 '13 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.