Last active
January 6, 2020 05:47
-
-
Save hluker/37b742be4f6cc4c629235bd8f8e38344 to your computer and use it in GitHub Desktop.
Remove various WordPress cruft
This file contains hidden or 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 | |
| add_filter( 'x_enqueue_parent_stylesheet', '__return_true' ); | |
| add_filter('xmlrpc_enabled', '__return_false'); | |
| 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 | |
| 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 Query Strings | |
| function remove_cssjs_ver( $src ) { | |
| if( strpos( $src, '?ver=' ) ) | |
| $src = remove_query_arg( 'ver', $src ); | |
| return $src; | |
| }; | |
| add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 ); | |
| add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 ); | |
| // Disable WP Embedding | |
| function disable_embed() { | |
| wp_dequeue_script( 'wp-embed' ); | |
| }; | |
| add_action( 'wp_footer', 'disable_embed' ); | |
| // No Self Pingback | |
| function disable_pingback( &$links ) { | |
| foreach ( $links as $l => $link ) | |
| if ( 0 === strpos( $link, get_option( 'home' ) ) ) | |
| unset($links[$l]); | |
| }; | |
| add_action( 'pre_ping', 'disable_pingback' ); | |
| // Limit WP Revisions | |
| define('WP_POST_REVISIONS', 10); | |
| // Disable WP Heartbeat | |
| add_action( 'init', 'stop_heartbeat', 1 ); | |
| function stop_heartbeat() { | |
| wp_deregister_script('heartbeat'); | |
| }; | |
| // Disable Dash Icons on Front End | |
| function wpdocs_dequeue_dashicon() { | |
| if (current_user_can( 'update_core' )) { | |
| return; | |
| }; | |
| wp_deregister_style('dashicons'); | |
| }; | |
| add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_dashicon' ); | |
| // Remove "Private: " and "Protected: " prefixes from WP | |
| function the_title_trim($title) { | |
| $title = attribute_escape($title); | |
| $findthese = array( | |
| '#Protected: #', // Notice the blank space after Protected: | |
| '#Private: #' // Notice again, otherwise the title get pushed. | |
| ); | |
| $replacewith = array( | |
| '', // What to replace "Protected: " with | |
| '' // What to replace "Private: " with | |
| ); | |
| $title = preg_replace($findthese, $replacewith, $title); | |
| return $title; | |
| } | |
| add_filter('the_title', 'the_title_trim'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment