By default, WordPress loads a lot of unnecessary boilerplate in the header, mostly via wp_head
.
The following functions remove a good number of unnecessary <head>
outputs, as well as removes some unnecessary CSS and JavaScript files. These changes can improve the speed and security of your WordPress theme.
The code snippets in this blog post perform a variety of WordPress clean-up functions:
- Removes a variety of unnecessary
<head>
metas and links - Removes the WordPress version from the front-end
- Disables all emoji functionality
- Disables unauthenticated requests to REST API routes
- Removes Gutenberg CSS
- Removes jQuery Migrate from the front-end
- Removes the WordPress oEmbed script
Add any of the functions below to your functions.php
file to remove some of these unnecessary WordPress output items.
Remove unnecessary wp_head actions and output
/** * Remove unnecessary wp_head actions and output */ remove_action ( 'wp_head', 'rsd_link' ); // remove xml rpc remove_action( 'wp_head', 'wlwmanifest_link' ); // remove manifest remove_action( 'wp_head', 'wp_shortlink_wp_head' ); // remove shortlink remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ); remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
Remove WordPress version from markup
/** * Remove the output of WP version */ function remove_wp_version() { return ''; } add_filter( 'the_generator', 'remove_wp_version' ) ;
Disable Emojis and remove related scripts
/** * Disable emojis */ function disable_emojis() { 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', 'disable_emojis_tinymce' ); add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 ); add_filter( 'emoji_svg_url', '__return_false' ); // remove dns prefetch for emoji } add_action( 'init', 'disable_emojis' ); /** * Filter function used to remove the tinymce emoji plugin. * * @param array $plugins * @return array Difference betwen the two arrays */ function disable_emojis_tinymce( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } else { return array(); } } /** * Remove emoji CDN hostname from DNS prefetching hints. * * @param array $urls URLs to print for resource hints. * @param string $relation_type The relation type the URLs are printed for. * @return array Difference betwen the two arrays. */ function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) { if ( 'dns-prefetch' == $relation_type ) { /** This filter is documented in wp-includes/formatting.php */ $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ); $urls = array_diff( $urls, array( $emoji_svg_url ) ); } return $urls; }
Disable unauthenticated REST API requests
/** * Disable unauthenticated REST API requests */ function disable_unauthenticated_rest( $result ) { // If a previous authentication check was applied, pass that result along without modification. if ( true === $result || is_wp_error( $result ) ) { return $result; } // disable response if not logged in if ( ! is_user_logged_in() ) { return new \WP_Error( 'access_denied', __( 'Access denied' ), array( 'status' => 401 ) ); } return $result; } add_filter( 'rest_authentication_errors', 'disable_unauthenticated_rest' );
Remove oEmbed script
/** * Remove oembed script */ function remove_oembed_script(){ wp_deregister_script( 'wp-embed' ); } add_action( 'wp_footer', 'remove_oembed_script' );
Remove Gutenberg block CSS
/** * Remove Gutenberg CSS * Obviously you should not add this if you are using Gutenberg blocks */ function remove_block_css() { wp_dequeue_style( 'wp-block-library' ); } add_action( 'wp_print_styles', 'remove_block_css', 100 );
Remove jQuery Migrate
/** * Remove jQuery Migrate */ function remove_jquery_migrate( $scripts ) { if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) { $script = $scripts->registered['jquery']; if ( $script->deps ) { $script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) ); } } } add_action( 'wp_default_scripts', 'remove_jquery_migrate' );
Note, make sure you do not need any of these items in your theme, plugins, or somewhere else on your WordPress site before implementing them.