Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active July 15, 2019 13:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mikehins/40c418d9de49fbf3af5cf88b5a22069f to your computer and use it in GitHub Desktop.
Remove unecessary stuff from wordpress
<?php
/**
* Created by PhpStorm.
* User: mikeh
* Date: 2018-12-19
* Time: 10:28 AM
*/
class HouseKeeping
{
public $noScriptDefer = [
'jquery.js',
];
public function init()
{
add_filter('style_loader_src', [$this, 'removeCssJsVersions'], 9999);
add_filter('script_loader_src', [$this, 'removeCssJsVersions'], 9999);
$this->deferJavascript();
$this->disablesUnnecessaryFunctionalities();
$this->addSecutityHeaders();
//$this->disableAPI();
}
public function removeCssJsVersions($src)
{
if (strpos($src, 'ver=')) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
public function deferJavascript()
{
if (is_admin()) {
return false;
}
add_filter('clean_url', function ($url) {
if (!strpos($url, '.js')) {
return $url;
}
if (strpos($url, 'jquery.js')) {
return $url;
}
if (strpos($url, 'recaptcha/api.js')) {
return $url;
}
return $url . '\'' . ' defer=\'defer';
}, 11, 1);
}
public function disablesUnnecessaryFunctionalities()
{
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
remove_action('auth_cookie_malformed', 'rest_cookie_collect_status');
remove_action('auth_cookie_expired', 'rest_cookie_collect_status');
remove_action('auth_cookie_bad_username', 'rest_cookie_collect_status');
remove_action('auth_cookie_bad_hash', 'rest_cookie_collect_status');
remove_action('auth_cookie_valid', 'rest_cookie_collect_status');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
remove_filter('comment_text', 'make_clickable', 9);
add_filter('wp_calculate_image_srcset_meta', '__return_null');
add_filter('wp_calculate_image_sizes', '__return_false', 99);
remove_filter('the_content', 'wp_make_content_images_responsive');
add_filter('wp_get_attachment_image_attributes', function ($attr) {
foreach (array('sizes', 'srcset') as $key) {
if (isset($attr[$key])) {
unset($attr[$key]);
}
}
return $attr;
}, 99);
add_action('init', function () {
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
add_filter('tiny_mce_plugins', function ($plugins) {
if (is_array($plugins)) {
return array_diff($plugins, array('wpemoji'));
} else {
return array();
}
});
add_filter('wp_resource_hints', function ($urls, $relation_type) {
if ('dns-prefetch' == $relation_type) {
$emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2.2.1/svg/');
$urls = array_diff($urls, array($emoji_svg_url));
}
return $urls;
}, 10, 2);
});
add_filter('emoji_svg_url', '__return_false');
add_action('widgets_init', function () {
global $wp_widget_factory;
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
});
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'start_post_rel_link', 10);
remove_action('wp_head', 'parent_post_rel_link', 10);
remove_action('wp_head', 'adjacent_posts_rel_link', 10);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'pagenavi_css');
add_filter('xmlrpc_methods', function ($methods) {
unset($methods['pingback.ping']);
unset($methods['pingback.extensions.getPingbacks']);
return $methods;
});
add_filter('wp_headers', function ($headers) {
unset($headers['X-Pingback']);
return $headers;
});
}
public function disableAPI()
{
add_filter('rest_enabled', '__return_false');
remove_action('wp_head', 'rest_output_link_wp_head', 10, 0);
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');
remove_filter('rest_authentication_errors', 'rest_cookie_check_errors', 100);
remove_action('init', 'rest_api_init');
remove_action('rest_api_init', 'rest_api_default_filters', 10, 1);
remove_action('parse_request', 'rest_api_loaded');
remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
}
public function addSecutityHeaders()
{
add_action('send_headers', function () {
header( 'Strict-Transport-Security: max-age=10886400; includeSubDomains; preload' );
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
});
}
}
(new HouseKeeping)->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment