Skip to content

Instantly share code, notes, and snippets.

@enniosousa
Last active March 30, 2023 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save enniosousa/89c431db61ae85fefe770db58864eeff to your computer and use it in GitHub Desktop.
Save enniosousa/89c431db61ae85fefe770db58864eeff to your computer and use it in GitHub Desktop.
Clear Wordpress head removing meta tags, styles and scripts
<?php
/*
* Name: wordpress assist clean header
* Source: https://gist.github.com/Auke1810/f2a4cf04f2c07c74a393a4b442f22267
* Date: 2020-07-25
*/
remove_action('wp_head', 'rsd_link'); // remove really simple discovery link
remove_action('wp_head', 'wp_generator'); // remove wordpress version
remove_action('wp_head', 'feed_links', 2); // remove rss feed links (make sure you add them in yourself if youre using feedblitz or an rss service)
remove_action('wp_head', 'feed_links_extra', 3); // removes all extra rss feed links
remove_action('wp_head', 'index_rel_link'); // remove link to index page
remove_action('wp_head', 'wlwmanifest_link'); // remove wlwmanifest.xml (needed to support windows live writer)
remove_action('wp_head', 'start_post_rel_link', 10, 0); // remove random post link
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // remove parent post link
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // remove the next and previous post links
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); // Remove shortlink
/*
* Remove JSON API links in header html
*/
function remove_json_api()
{
// Remove the REST API lines from the HTML Header
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
// Remove the REST API endpoint.
remove_action('rest_api_init', 'wp_oembed_register_route');
// Turn off oEmbed auto discovery.
add_filter('embed_oembed_discover', '__return_false');
// Don't filter oEmbed results.
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
// Remove oEmbed discovery links.
remove_action('wp_head', 'wp_oembed_add_discovery_links');
// Remove oEmbed-specific JavaScript from the front-end and back-end.
remove_action('wp_head', 'wp_oembed_add_host_js');
// Remove all embeds rewrite rules.
add_filter('rewrite_rules_array', 'disable_embeds_rewrites');
}
add_action('after_setup_theme', 'remove_json_api');
/*
Snippet completely disable the REST API and shows {"code":"rest_disabled","message":"The REST API is disabled on this site."}
when visiting http://yoursite.com/wp-json/
*/
function disable_json_api()
{
// Filters for WP-API version 1.x
add_filter('json_enabled', '__return_false');
add_filter('json_jsonp_enabled', '__return_false');
// Filters for WP-API version 2.x
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
}
add_action('after_setup_theme', 'disable_json_api');
/*
* Remove WP resources hints
* Exemple: <link rel="dns-prefetch" href="//s.w.org" />
*/
// remove_action('wp_head', 'wp_resource_hints', 2);
/*
* Remove WP defatult scripts
*/
function change_default_jquery()
{
wp_dequeue_script('jquery');
wp_deregister_script('jquery');
wp_dequeue_style('wp-block-library');
}
// add_filter('wp_enqueue_scripts', 'change_default_jquery', PHP_INT_MAX);
/*
* remove hello elementor style
*/
// add_action('hello_elementor_enqueue_style', '__return_false');
// add_action('hello_elementor_enqueue_theme_style', '__return_false');
// Remove Elementor Icons (Eicons)
function remove_default_stylesheet()
{
wp_deregister_style('elementor-icons');
wp_deregister_style('elementor-animations');
wp_deregister_style('elementor-frontend');
}
// add_action('wp_enqueue_scripts', 'remove_default_stylesheet', 20);
// Remove Google Fonts
// add_filter('elementor/frontend/print_google_fonts', '__return_false');
// Remove Font Awesome
function remove_elementor_fa_icons()
{
foreach ([ 'solid', 'regular', 'brands' ] as $style) {
wp_deregister_style('elementor-icons-fa-' . $style);
}
}
// add_action('elementor/frontend/after_register_styles', 'remove_elementor_fa_icons', 20);

On Wordpress active theme folder, append to functions.php the following code

include_once __DIR__ . DIRECTORY_SEPARATOR . 'clean-header.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment