-
-
Save GaryJones/7dd2ab721f4d97ff6502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Filename-based cache busting | |
* Version: 0.3 | |
* Description: Filename-based cache busting for WordPress scripts/styles. | |
* Author: Dominik Schilling | |
* Author URI: http://wphelper.de/ | |
* Plugin URI: http://wpgrafie.de/880/ | |
* | |
* License: GPLv2 or later | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* | |
* | |
* Extend your .htaccess file with these lines: | |
* | |
* <IfModule mod_rewrite.c> | |
* RewriteEngine On | |
* RewriteBase / | |
* | |
* RewriteCond %{REQUEST_FILENAME} !-f | |
* RewriteCond %{REQUEST_FILENAME} !-d | |
* RewriteRule ^(.+)\.(.+)\.(js|css)$ $1.$3 [L] | |
* </IfModule> | |
*/ | |
/** | |
* Removes the `ver` query string of the source and places it into | |
* the filename. Doesn't change admin scripts/styles and sources | |
* with more than the `ver` arg. | |
* | |
* @param string $src The original source | |
* @return string | |
*/ | |
function ds_filename_based_cache_busting( $src ) { | |
// Don't touch admin scripts | |
if ( is_admin() || false === strstr( $src, home_url() ) ) { | |
return $src; | |
} | |
return preg_replace( | |
'/\.(js|css)\?ver=(.+)$/', | |
'.$2.$1', | |
$src | |
); | |
} | |
add_filter( 'script_loader_src', 'ds_filename_based_cache_busting' ); | |
add_filter( 'style_loader_src', 'ds_filename_based_cache_busting' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment