Removing excess from WordPress
Removing unnecessary or outdated WordPress functionality – refers to the process of eliminating features, components, or code within a WordPress website that are either no longer needed, obsolete, or may pose a security risk. This practice is done to streamline the website, improve performance, enhance security, and maintain a more efficient and optimized WordPress installation.
Some reasons to remove unnecessary or outdated functionality in WordPress include:
Performance Optimization: Reducing unnecessary features can improve the overall speed and performance of the website, leading to better user experiences and search engine rankings.
Security Enhancement: Removing outdated or unused functionalities reduces potential vulnerabilities, minimizing the risk of security breaches and unauthorized access.
Simplified Maintenance: Eliminating unnecessary features makes it easier to manage and maintain the WordPress website. It reduces the complexity of the codebase and simplifies updates and troubleshooting.
Improved User Experience: Streamlining the website by removing unnecessary elements can enhance the user experience by decluttering the interface and making it more user-friendly.
Reduced Server Load: Unnecessary functionalities can contribute to increased server resource usage. Removing them can reduce the load on the server, leading to more efficient resource utilization.
Examples of unnecessary or outdated functionalities that might be removed include unused plugins, outdated themes, deprecated code, and features that are no longer relevant to the website’s goals or objectives. It’s essential to carefully assess each component before removal to ensure that it doesn’t impact the website’s core functionality or user experience negatively.
This practice is part of ongoing website maintenance and optimization efforts to keep a WordPress site secure, efficient, and aligned with current best practices. Regularly auditing and updating the website’s functionalities contribute to a healthier and more resilient online presence.
Let's start deleting
If you’ve been developing a website or promoting it for several days, you’ve probably noticed that WordPress adds quite a lot of extra code, especially for the <head> block.
There are two options for how to get rid of unnecessary pieces of code in WordPress:
- Installing a plugin – there are many plugins that solve a specific problem; over time, more than a dozen of them will accumulate. Or installing one plugin such as Clearfy Pro – but this plugin is paid and it may not have the functions that we need now or may need in the future.
- We are creating our own universal solution – a file that will eliminate all unnecessary content and in the future we will be able to supplement it and install it on any sites – this is the way of a working developer!!
First, I will list all the tasks point by point, and by the end I will give you my file, which I use on most projects. All the examples below need to be added to the functions.php file of your theme; if your theme is being updated, you need to create a child theme.
Removing the WP version from the site header
WordPress generates the generator meta tag in the <head> section by default
<meta name="generator" content="WordPress x.x.x" />
To remove this code, use just one line.remove_action('wp_head','wp_generator');
Removing links to RSS feeds
CMS WordPress by default creates several RSS feeds for the site, if you do not plan to use them, then I advise you to remove links to feeds and the functions that generate them:
function fb_disable_feed(){wp_redirect(get_option('siteurl'));}
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'rsd_link' );
add_action( 'do_feed', 'fb_disable_feed', 1 );
add_action( 'do_feed_rdf', 'fb_disable_feed', 1 );
add_action( 'do_feed_rss', 'fb_disable_feed', 1 );
add_action( 'do_feed_rss2', 'fb_disable_feed', 1 );
add_action( 'do_feed_atom', 'fb_disable_feed', 1 );
Disable REST API
The REST API facilitates data exchange between your website and external services or applications. For instance, creating a phone app for managing, receiving, and modifying website data. This functionality is usually used to connect different APIs with each other. If this is your first time hearing about this, then feel free to insert the lines below into your function.php file:
add_filter( 'rest_enabled', '__return_false' );
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
remove_action( 'template_redirect', 'rest_output_link_header', 11);
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_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 );
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 );
Disable Emoji
Outdated functionality that replaces text emoticons with WordPress’s own Emoji package, since now all browsers have native support, there is no need for Emoji from WordPress, by disabling them your site will thank you, since now it will not load unnecessary scripts when loading pages and images.
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' );
Cancel srcset
This is an attribute of the <img srcset> tag – a replacement for the standard src. The scrset specifies several image URLs and the condition under which it should be used, most often this is the screen resolution, that is, different images will be shown at different resolutions. In WordPress, this is exactly the type of image output that is enabled by default, but the downside is the larger amount of html code since the srcset often contains quite a lot of text. I recommend everyone to disable this function, with these lines:
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' );
Disabling Gutenberg
Gutenberg is a visual editor for WordPress pages and posts, quite convenient and flexible and constantly updated, but there are a couple of critical disadvantages.
Along with convenience, we get extra style sheets and scripts, which negatively affects loading.
I am for the uniqueness of the code, and in the case of any visual editors, be it Elementor or Visual Composer, we get pages that, both visually and html code, will be similar to a million others on the Internet.
The code below will completely disable Gutenberg, which is enabled by default in WordPress, and will also remove the styles and scripts it uses:
add_filter('use_block_editor_for_post_type', '__return_false', 100);
add_action('admin_init', function() {
remove_action('admin_notices', ['WP_Privacy_Policy_Content', 'notice']);
add_action('edit_form_after_title', ['WP_Privacy_Policy_Content', 'notice']);
});
function gut_style_disable() { wp_dequeue_style('wp-block-library'); }
add_action('wp_enqueue_scripts', 'gut_style_disable', 100);
Disabling XML-RPC
XML Remote Procedure Call – used to connect to a site through the WordPress mobile application, it also notifies the owner that a link to his site has been left somewhere. If you do not use this functionality, feel free to disable it:add_filter('xmlrpc_enabled', '__return_false');
Add to file .htaccess:<Files xmlrpc.php>
Order Deny,Allow
Deny from all
Allow from 192.168.1.1
Allow from 10.0.0.0/100
</Files>
Disable JQuery Migrate
The built-in jquery-migrate.min.js file was previously used for jQuery version compatibility. Now there is practically no need to use this file.
function isa_remove_jquery_migrate( &$scripts ) {
if( !is_admin() ){
$scripts->remove( 'jquery' ); $scripts->add( 'jquery', false, array( 'jquery-core' ), '1.12.4' );
}
}
add_filter( 'wp_default_scripts', 'isa_remove_jquery_migrate' );
Remove WP version from .css and .js files
Initially, for all scripts and styles, WordPress writes ?ver=X.XX at the end of each included file; for security and code purity reasons, it is advisable to remove these parameters, this is done with the following code:
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
function remove_cssjs_ver( $src ) {
if( strpos($src,'?ver='))
$src = remove_query_arg( 'ver', $src );
return $src;
}