Last active
November 19, 2017 12:28
-
-
Save elusiveunit/f0f728771de6647778fa to your computer and use it in GitHub Desktop.
WordPress editor style cache refresh plugin
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: Editor Styles Refresh | |
* Plugin URI: https://gist.github.com/elusiveunit/f0f728771de6647778fa | |
* Description: Adds file time query string to theme editor style URLs, to force cache busting when the file is changed. | |
* Author: Jens Lindberg | |
* Version: 0.1 | |
* License: GPL-2.0+ | |
*/ | |
/** | |
* Add query string with file modification timestamp to theme editor style | |
* files. | |
* | |
* @param string $css Comma separated stylesheet URIs. | |
* @return string | |
*/ | |
function esrjl_refresh_editor_styles_sweet_unique_func_name_bro( $css ) { | |
$files = explode( ',', $css ); | |
$tpl_uri = get_template_directory_uri(); | |
$style_uri = get_stylesheet_directory_uri(); | |
foreach ( $files as $file_url ) : | |
// Not a theme file, skip it | |
if ( false === strpos( $file_url, $tpl_uri ) | |
&& false === strpos( $file_url, $style_uri ) ) | |
continue; | |
$file_data = parse_url( $file_url ); | |
$file_path = ( ! empty( $file_data['path'] ) ) ? $file_data['path'] : ''; | |
// Assuming this could fail on some server setups | |
$file_full_path = ( $file_path ) ? $_SERVER['DOCUMENT_ROOT'] . $file_path : ''; | |
if ( empty( $file_data['query'] ) | |
&& $file_full_path | |
&& file_exists( $file_full_path ) ) : | |
$mod_time = filemtime( $file_full_path ); | |
// Add modification timestamp query string | |
if ( is_int( $mod_time ) ) : | |
$css = str_replace( | |
$file_path, | |
"{$file_path}?ver={$mod_time}", | |
$css | |
); | |
endif; | |
endif; | |
endforeach; | |
return $css; | |
} | |
add_filter( 'mce_css', 'esrjl_refresh_editor_styles_sweet_unique_func_name_bro' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment