Created
April 17, 2025 02:54
-
-
Save arenagroove/cfbcd98bfb6f49c8d97f0412c68317f2 to your computer and use it in GitHub Desktop.
MU Plugin to enable or disable speculative loading introduced in WordPress 6.8.
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: LR Speculative Loading Toggle (UI) | |
| * Description: Adds admin UI to enable or disable speculative loading introduced in WordPress 6.8. | |
| * Version: 1.5 | |
| * Author: Luis Martinez | |
| * Author URI: https://www.lessrain.com | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; // Prevent direct access | |
| } | |
| /** | |
| * Initialize default setting (disabled = true) on first load | |
| */ | |
| add_action( 'init', function() { | |
| if ( get_option( 'lr_disable_speculative_loading' ) === false ) { | |
| add_option( 'lr_disable_speculative_loading', true ); | |
| } | |
| } ); | |
| /** | |
| * Apply the speculative loading filter | |
| * Must return null to disable speculative loading (per WP core) | |
| */ | |
| add_filter( 'wp_speculation_rules_configuration', 'lr_speculative_loading_filter', 1 ); | |
| function lr_speculative_loading_filter( $config ) { | |
| $disabled = get_option( 'lr_disable_speculative_loading', true ); | |
| return $disabled ? null : $config; | |
| } | |
| /** | |
| * Add admin settings page under "Settings" | |
| */ | |
| add_action( 'admin_menu', function() { | |
| add_options_page( | |
| 'LR Speculative Loading', | |
| 'LR Speculative Loading', | |
| 'manage_options', | |
| 'lr-speculative-loading-settings', | |
| 'lr_render_speculative_loading_settings_page' | |
| ); | |
| } ); | |
| /** | |
| * Register the setting with strict boolean sanitization | |
| */ | |
| add_action( 'admin_init', function() { | |
| register_setting( | |
| 'lr_speculative_loading_settings_group', | |
| 'lr_disable_speculative_loading', | |
| [ | |
| 'type' => 'boolean', | |
| 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 'default' => true, | |
| ] | |
| ); | |
| } ); | |
| /** | |
| * Render the admin settings page UI | |
| */ | |
| function lr_render_speculative_loading_settings_page() { | |
| $is_disabled = get_option( 'lr_disable_speculative_loading', true ); | |
| ?> | |
| <div class="wrap" style="max-width: 900px;"> | |
| <h1 style="margin-bottom:1rem;">LR Speculative Loading</h1> | |
| <form method="post" action="options.php"> | |
| <?php settings_fields( 'lr_speculative_loading_settings_group' ); ?> | |
| <?php do_settings_sections( 'lr_speculative_loading_settings_group' ); ?> | |
| <fieldset> | |
| <label for="lr_disable_speculative_loading"> | |
| <input type="checkbox" | |
| id="lr_disable_speculative_loading" | |
| name="lr_disable_speculative_loading" | |
| value="1" | |
| <?php checked( 1, $is_disabled ); ?> /> | |
| Disable speculative preloading | |
| </label> | |
| <p class="description"> | |
| Prevent WordPress from injecting speculative preloading scripts. Enabled by default. | |
| </p> | |
| </fieldset> | |
| <?php submit_button(); ?> | |
| </form> | |
| </div> | |
| <?php | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment