Last active
October 13, 2023 20:28
-
-
Save onecooltaco/87e7462e7abb3fd06bfc58730281f5f4 to your computer and use it in GitHub Desktop.
WordPress mu plugin to disable APIs in WordPress header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** | |
Plugin Name: disable APIs | |
Plugin URI: https://gist.github.com/onecooltaco/87e7462e7abb3fd06bfc58730281f5f4 | |
Description: Disable APIs in WordPress header | |
Version: 1.0.0 | |
Author: Jeremy Leggat | |
***/ | |
/* | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
// Remove Head API | |
remove_action('wp_head', 'rest_output_link_wp_head'); | |
remove_action('wp_head', 'wp_oembed_add_discovery_links'); | |
remove_action('template_redirect', 'rest_output_link_header', 11); | |
/** Disable REST 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'); | |
// More REST API Hooks | |
remove_action('rest_api_init', 'wp_oembed_register_route'); // Remove the REST API endpoint. | |
/** Disable Heartbeat API **/ | |
add_action( 'init', 'oct_disable_apis_stop_heartbeat', 1 ); | |
function oct_disable_apis_stop_heartbeat() { | |
wp_deregister_script('heartbeat'); | |
} | |
// Remove WordPress Version Number | |
remove_action('wp_head', 'wp_generator'); // Remove WordPress Generator Version | |
add_filter('the_generator', '__return_false'); // Remove Generator Name From Rss Feeds. | |
// Remove Feeds | |
remove_action('wp_head', 'feed_links_extra', 3); // Remove Every Extra Links to Rss Feeds. | |
remove_action('wp_head', 'feed_links', 2); | |
remove_action('wp_head', 'wc_products_rss_feed'); | |
/* Disable Self Pings */ | |
add_action('pre_ping', 'oct_disable_apis_no_self_ping'); | |
function oct_disable_apis_no_self_ping(&$links) | |
{ | |
$home = get_option('home'); | |
foreach ($links as $l => $link) | |
if (0 === strpos($link, $home)) | |
unset($links[$l]); | |
} | |
// Remove Link shortlink from http | |
remove_action('template_redirect', 'wp_shortlink_header', 11); | |
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); | |
// Remove X-Pingback | |
add_filter('wp_headers', 'oct_disable_apis_remove_pingback_head'); | |
add_action('wp', 'oct_disable_apis_remove_pingback'); | |
function oct_disable_apis_remove_pingback_head($headers) | |
{ | |
if (isset($headers['X-Pingback'])) { | |
unset($headers['X-Pingback']); | |
} | |
return $headers; | |
} | |
function oct_disable_apis_remove_pingback() | |
{ | |
if (function_exists('header_remove')) { | |
header_remove('X-Pingback'); | |
} | |
} | |
// Remove date archives | |
add_action('template_redirect', 'oct_disable_apis_remove_archives_date'); | |
function oct_disable_apis_remove_archives_date() | |
{ | |
if (is_date()){ | |
$target = get_option('siteurl'); | |
$status = '301'; | |
wp_redirect($target, 301); | |
die(); | |
} | |
} | |
// Remove Author archives | |
add_action('template_redirect', 'oct_disable_apis_remove_archives_author'); | |
function oct_disable_apis_remove_archives_author() | |
{ | |
if (is_author()){ | |
$target = get_option('siteurl'); | |
$status = '301'; | |
wp_redirect($target, 301); | |
die(); | |
} | |
} | |
// Remove tag archives | |
add_action('template_redirect', 'oct_disable_apis_remove_archives_tag'); | |
function oct_disable_apis_remove_archives_tag() | |
{ | |
if (is_tag()){ | |
$target = get_option('siteurl'); | |
$status = '301'; | |
wp_redirect($target, 301); | |
die(); | |
} | |
} | |
// Remove category archives | |
add_action('template_redirect', 'oct_disable_apis_remove_archives_category'); | |
function oct_disable_apis_remove_archives_category() | |
{ | |
if (is_category()){ | |
$target = get_option('siteurl'); | |
$status = '301'; | |
wp_redirect($target, 301); | |
die(); | |
} | |
} | |
// Remove Link to Home Page. | |
remove_action('wp_head', 'index_rel_link'); | |
// REMOVE wlwmanifest.xml. | |
remove_action('wp_head', 'wlwmanifest_link'); | |
// Remove Really Simple Discovery Link | |
remove_action('wp_head', 'rsd_link'); | |
/** Remove Canonical URL **/ | |
remove_action('wp_head', 'rel_canonical'); | |
add_filter('wpseo_canonical', '__return_false'); | |
// Remove Prev/Next Link from Head | |
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10); // Remove Prev-next Links From Header -not From Post-. | |
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // Remove Prev-next Links. | |
remove_action('wp_head', 'start_post_rel_link', 10, 0); // Remove Random Link Post. | |
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // Remove Parent Post Link. | |
// Disable XML-RPC | |
add_action('wp_default_scripts', 'oct_disable_apis_disable_xmlrpc', 9999); | |
function oct_disable_apis_disable_xmlrpc() | |
{ | |
add_filter('xmlrpc_enabled', '__return_false'); | |
} | |
// Remove search page | |
add_action('template_redirect', 'oct_disable_apis_remove_archives_search'); | |
function oct_disable_apis_remove_archives_search() | |
{ | |
if (is_search()){ | |
$target = get_option('siteurl'); | |
$status = '301'; | |
wp_redirect($target, 301); | |
die(); | |
} | |
} | |
/** Remove Version Query Strings from Scripts/Styles **/ | |
add_filter('script_loader_src', 'oct_disable_apis_remove_script_version', 15, 1); | |
add_filter('style_loader_src', 'oct_disable_apis_remove_script_version', 15, 1); | |
/** Remove Query Strings from Scripts/Styles **/ | |
function oct_disable_apis_remove_script_version($src) | |
{ | |
$parts = explode('?ver', $src); | |
return $parts[0]; | |
} | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment