Jake Vanderwerf
2026-02-11 3b3bd067d0ff2671fca2890c14428c97e1011a2b
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
/**
 * WordPress Cleanup and Optimization Functions
 *
 * Removes unnecessary WordPress features, optimizes performance,
 * and improves security by removing potential attack vectors.
 */
 
/*******************************************************************************
BLOCK EDITOR & STYLES CLEANUP
*******************************************************************************/
 
/**
 * Remove WordPress block editor styles and scripts
 * Cleans up unnecessary block-related assets for custom themes
 */
function jvbRemoveBlockAssets(): void
{
    if (is_admin()) {
        return;
    }
 
    // Remove global WordPress styles
    $global_styles = [
        'global-styles',
        'classic-theme-styles',
        'core-block-supports',
        'dashicons',
        'common',
        'wp-block-library',
        'wp-block-library-theme',
        'wp-block-styles',
        'block-style-variation-styles'
    ];
 
    foreach ($global_styles as $style) {
        wp_dequeue_style($style);
        wp_deregister_style($style);
    }
 
    // Remove all block-specific styles
    global $wp_styles;
    foreach ($wp_styles->queue as $handle) {
        if (str_starts_with($handle, 'wp-block-')) {
            wp_dequeue_style($handle);
            wp_deregister_style($style);
        }
    }
 
    // Remove block-specific scripts and modules
    if (!is_admin()) {
        wp_deregister_script('heartbeat');
    }
    wp_dequeue_script('wp-block-template-skip-link');
 
    // Remove WordPress 6.5+ script modules
    wp_dequeue_script_module('@wordpress/interactivity');
    wp_deregister_script_module('@wordpress/interactivity');
    wp_dequeue_script_module('@wordpress/block-library/navigation/view');
    wp_deregister_script_module('@wordpress/block-library/navigation/view');
 
    // Remove block template skip link
    remove_action('wp_footer', 'the_block_template_skip_link');
 
    // Remove third-party styles
    wp_deregister_style('akismet-widget-style-inline-css');
}
add_action('wp_enqueue_scripts', 'jvbRemoveBlockAssets', 9999);
 
/*******************************************************************************
WORDPRESS HEAD CLEANUP
*******************************************************************************/
 
/**
 * Remove unnecessary WordPress head elements
 * Cleans up HTML head for better performance and security
 */
function jvbCleanWordPressHead(): void
{
    // Remove version info and meta tags
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    remove_action('wp_head', 'wp_shortlink_wp_head');
 
    // Remove emoji support
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('wp_print_styles', 'print_emoji_styles');
 
    // Remove REST API and oEmbed discovery links
    remove_action('wp_head', 'rest_output_link_wp_head');
    remove_action('template_redirect', 'rest_output_link_header', 11);
    remove_action('wp_head', 'wp_oembed_add_discovery_links');
    remove_action('wp_head', 'wp_oembed_add_host_js');
}
add_action('init', 'jvbCleanWordPressHead');
 
/*******************************************************************************
SECURITY OPTIMIZATIONS
*******************************************************************************/
 
/**
 * Apply security hardening measures
 * Removes version info, disables XML-RPC, and hardens WordPress
 */
function jvbSecurityOptimizations(): void
{
    // Hide WordPress version from scripts and styles
    add_filter('style_loader_src', 'jvbRemoveVersionFromUrl', 9999);
    add_filter('script_loader_src', 'jvbRemoveVersionFromUrl', 9999);
 
    // Disable XML-RPC (often targeted by attackers)
    add_filter('xmlrpc_enabled', '__return_false');
 
    // Remove security headers that reveal info
    add_filter('wp_headers', 'jvbRemovePingbackHeader');
 
    // Remove WordPress version from admin footer
    add_filter('update_footer', '__return_empty_string', 11);
 
    // Block user enumeration
    add_action('init', 'jvbBlockUserEnumeration');
    add_filter('rest_endpoints', 'jvbDisableUserEndpoints');
    add_filter('author_rewrite_rules', '__return_empty_array');
 
    // Disable file editing in admin (if not already set)
    if (!defined('DISALLOW_FILE_EDIT')) {
        define('DISALLOW_FILE_EDIT', true);
    }
 
    // Shorten password reset expiration (15 minutes)
    add_filter('password_reset_expiration', function() {
        return 900;
    });
}
add_action('init', 'jvbSecurityOptimizations');
 
/**
 * Remove version query strings from static assets
 */
function jvbRemoveVersionFromUrl(string $src): string
{
    return strpos($src, 'ver=') ? remove_query_arg('ver', $src) : $src;
}
 
/**
 * Remove X-Pingback header
 */
function jvbRemovePingbackHeader(array $headers): array
{
    unset($headers['X-Pingback']);
    return $headers;
}
 
/**
 * Block user enumeration attempts
 * Prevents discovery of usernames via URL manipulation
 */
function jvbBlockUserEnumeration(): void
{
    // Block ?author queries and author archives
    if (!is_admin() && (isset($_GET['author']) || is_author())) {
        wp_redirect(home_url(), 301);
        exit;
    }
}
 
/**
 * Remove user endpoints from REST API
 * Prevents user enumeration via REST API
 */
function jvbDisableUserEndpoints(array $endpoints): array
{
    $user_endpoints = [
        '/wp/v2/users',
        '/wp/v2/users/(?P<id>[\d]+)'
    ];
 
    foreach ($user_endpoints as $endpoint) {
        if (isset($endpoints[$endpoint])) {
            unset($endpoints[$endpoint]);
        }
    }
 
    return $endpoints;
}
 
/*******************************************************************************
CONTENT MANAGEMENT
*******************************************************************************/
 
/**
 * Clean up attachments when posts are deleted
 * Automatically removes associated media to prevent orphaned files
 */
function jvbCleanupPostDeletion(int $post_id): void
{
    // Delete attached media
    $attachments = get_attached_media('', $post_id);
    foreach ($attachments as $attachment) {
        wp_delete_attachment($attachment->ID, true);
    }
 
    // Delete custom meta attachments
    $meta_fields = [
        BASE . 'image',
        BASE . 'gallery'
    ];
 
    //TODO: Dynamically use Meta.php to get any image or gallery fields
    foreach ($meta_fields as $meta_key) {
        $meta_value = get_post_meta($post_id, $meta_key, true);
 
        if (empty($meta_value)) {
            continue;
        }
 
        // Handle comma-separated IDs
        $attachment_ids = str_contains($meta_value, ',')
            ? explode(',', $meta_value)
            : [$meta_value];
 
        foreach ($attachment_ids as $attachment_id) {
            if (is_numeric($attachment_id)) {
                wp_delete_attachment((int)$attachment_id, true);
            }
        }
    }
}
add_action('before_delete_post', 'jvbCleanupPostDeletion');
 
/*******************************************************************************
THEME CUSTOMIZATION
*******************************************************************************/
 
/**
 * Clean up body classes for simpler CSS targeting
 * Removes WordPress defaults and adds only relevant classes
 */
function jvbBodyClasses(array $classes): array
{
    // Start with empty array - only add what we need
    $clean_classes = [];
 
    // Add contextual classes
    if (is_front_page()) {
        $clean_classes[] = 'home';
    }
 
    if (function_exists('jvbIsDirectory') && jvbIsDirectory()) {
        $clean_classes[] = 'is-directory';
    } elseif (is_tax()) {
        $clean_classes[] = str_replace(BASE, '', get_queried_object()->taxonomy);
    } elseif (is_singular() && !is_singular('page')) {
        $clean_classes[] = str_replace(BASE, '', get_queried_object()->post_type);
    } elseif (is_post_type_archive()) {
        $clean_classes[] = str_replace(BASE, '', get_queried_object()->name);
    }
 
    return $clean_classes;
}
add_filter('body_class', 'jvbBodyClasses');