Last active
May 12, 2026 21:19
-
-
Save chrisdavidmiles/aeb6b4cfe4246c3c95c45f9a40d9c74b to your computer and use it in GitHub Desktop.
WordPress MU plugin: auto-reloads when CSS/JS files 403 due to Apache inode cache lag
This file contains hidden or 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: 403 Reload Workaround | |
| * Description: Auto-reloads when CSS/JS files 403 due to Apache inode cache lag. | |
| * Plugin URI: https://gist.github.com/chrisdavidmiles/aeb6b4cfe4246c3c95c45f9a40d9c74b | |
| * Version: 0.1 | |
| * Author: Chris Miles | |
| */ | |
| add_action('init', function() { | |
| if (!is_admin() || !current_user_can('manage_options')) return; | |
| if (!isset($_GET['reload_403_toggle'])) return; | |
| check_admin_referer('reload_403_toggle'); | |
| $new = ($_GET['reload_403_toggle'] === 'disable') ? 'disabled' : 'enabled'; | |
| update_option('reload_403_status', $new); | |
| wp_safe_redirect(remove_query_arg(['reload_403_toggle', '_wpnonce'])); | |
| exit; | |
| }); | |
| add_filter('plugin_action_links_' . plugin_basename(__FILE__), function($links) { | |
| $disabled = get_option('reload_403_status') === 'disabled'; | |
| if ($disabled) { | |
| $url = wp_nonce_url(add_query_arg('reload_403_toggle', 'enable'), 'reload_403_toggle'); | |
| $links[] = '<a href="' . esc_url($url) . '">Enable</a>'; | |
| } else { | |
| $url = wp_nonce_url(add_query_arg('reload_403_toggle', 'disable'), 'reload_403_toggle'); | |
| $links[] = '<a href="' . esc_url($url) . '">Disable</a>'; | |
| } | |
| return $links; | |
| }); | |
| if (get_option('reload_403_status') === 'disabled') return; | |
| add_action('wp_head', 'reload_403_inline_script', 1); | |
| add_action('admin_head', 'reload_403_inline_script', 1); | |
| function reload_403_inline_script() { | |
| ?> | |
| <script> | |
| (function() { | |
| var key = '_403rr_' + location.pathname; | |
| var state = parseInt(sessionStorage.getItem(key) || '0', 10); | |
| var tsKey = key + '_t'; | |
| var ts = parseInt(sessionStorage.getItem(tsKey) || '0', 10); | |
| if (ts && Date.now() - ts > 30000) { | |
| state = 0; | |
| sessionStorage.removeItem(key); | |
| sessionStorage.removeItem(tsKey); | |
| } | |
| if (state === 1) { | |
| sessionStorage.setItem(key, '2'); | |
| setTimeout(function() { location.reload(); }, 2000); | |
| return; | |
| } | |
| if (state >= 2) { | |
| sessionStorage.removeItem(key); | |
| sessionStorage.removeItem(tsKey); | |
| return; | |
| } | |
| function check() { | |
| var entries = performance.getEntriesByType('resource'); | |
| for (var i = 0; i < entries.length; i++) { | |
| var e = entries[i]; | |
| if (e.responseStatus === 403 && /\.(css|js)(\?|$)/i.test(e.name)) { | |
| sessionStorage.setItem(key, '1'); | |
| sessionStorage.setItem(tsKey, String(Date.now())); | |
| location.reload(); | |
| return; | |
| } | |
| } | |
| } | |
| if (document.readyState === 'complete') { | |
| check(); | |
| } else { | |
| window.addEventListener('load', check); | |
| } | |
| })(); | |
| </script> | |
| <?php | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment