-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeculationrules-toggle.php
More file actions
72 lines (57 loc) · 2.15 KB
/
speculationrules-toggle.php
File metadata and controls
72 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
Plugin Name: Speculationrules Toggle (Audit Mode Focused)
Description: Simple plugin to disable or enable speculationrules output for accurate Lighthouse audits. Note:do not leave this on or you will ruin your Core Web Vitals. Thank you.
Version: 1.0
Author: by Serafin Tech
**
**
-------- CLI Commands ---------
#Enable Rules (checkbox is checked)
wp speculationrules enable
# Then open the browser in Incognito
# Run Lighthouse
# Restore life back to an optimized site (checkbox is unchecked)
wp speculationrules disable
------------------------------
**
*/
// Add the admin checkbox
add_action('admin_init', function() {
add_settings_field(
'speculationrules_audit_mode',
'Enable Audit Mode (Hide speculationrules)',
function() {
$value = get_option('speculationrules_audit_mode', 'no');
echo '<input type="checkbox" name="speculationrules_audit_mode" value="yes" ' . checked('yes', $value, false) . '> Enable Audit Mode (hide speculationrules)';
},
'reading'
);
register_setting('reading', 'speculationrules_audit_mode');
});
// Apply the logic
add_action('init', function() {
if (get_option('speculationrules_audit_mode') === 'yes') {
// Audit Mode is ENABLED -> hide speculationrules
add_filter('wp_speculative_loading_enabled', '__return_false');
remove_action('wp_head', 'wp_output_speculationrules_script');
add_action('template_redirect', function () {
ob_start(function ($html) {
return preg_replace('/<script type="speculationrules".*?<\\/script>/is', '', $html);
});
});
}
});
// CLI commands to match
if (defined('WP_CLI') && WP_CLI) {
WP_CLI::add_command('speculationrules', new class {
public function enable() {
update_option('speculationrules_audit_mode', 'yes');
WP_CLI::success('Audit Mode ENABLED. Speculationrules are now hidden.');
}
public function disable() {
update_option('speculationrules_audit_mode', 'no');
WP_CLI::success('Audit Mode DISABLED. Speculationrules are now visible.');
}
});
}