Skip to content

Instantly share code, notes, and snippets.

@Auke1810
Last active January 22, 2024 10:14
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save Auke1810/f2a4cf04f2c07c74a393a4b442f22267 to your computer and use it in GitHub Desktop.
Save Auke1810/f2a4cf04f2c07c74a393a4b442f22267 to your computer and use it in GitHub Desktop.
create a clean wordpress header and remove unnecessary clutter.
<?php
/*
Plugin Name: wordpress assist clean header
Plugin URI: http://www.wordpressassist.nl/
Description: Remove shortlink hook
Version: 1.0
Author: AukeJomm
Author URI: http://www.aukejongbloed.nl
*/
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' );
@jessuppi
Copy link

Thanks Auke! One of the best lists I've seen.

We were inspired to make a free plugin recently, would appreciate any feedback:

https://wordpress.org/plugins/header-cleanup-littlebizzy/

@toptools
Copy link

[error] 24281#24281: *501 FastCGI sent in stderr: "PHP message: PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'disable_embeds_rewrites' not found or invalid function name in /usr/share/nginx/html/bbb/wp-includes/class-wp-hook.php on line 286" whilereading response header from upstream

@pandymic
Copy link

pandymic commented Oct 8, 2021

[error] 24281#24281: *501 FastCGI sent in stderr: "PHP message: PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'disable_embeds_rewrites' not found or invalid function name in /usr/share/nginx/html/bbb/wp-includes/class-wp-hook.php on line 286" whilereading response header from upstream

It appears that you're adding a filter here, but there is no matching function named "disable_embeds_rewrites" being declared.

A quick Google search returns a Kinsta article that uses this same filter and has some more complete code.

function disable_embeds_rewrites($rules) {
    foreach($rules as $rule => $rewrite) {
        if(false !== strpos($rewrite, 'embed=true')) {
            unset($rules[$rule]);
        }
    }
    return $rules;
}

@iiic
Copy link

iiic commented Mar 23, 2022

Some parameters can be redundant like in remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); third param "priority" has 10 as default value, so you don't have to write it again.

@Deviant96
Copy link

Deviant96 commented Oct 1, 2023

wlwmanifest_link is deprecated in 6.3.0 and no longer included in core so it might be redundant to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment