My WordPress ‘cleanup’ script

I include the following, or something similar, in most of my WordPress projects. It standardises some awkward behaviours, removes some superfluous settings and capabilities, and enhances/improves some default functions and approaches.

Warning – do not simply copy and paste this into your theme/functions; it’ll need adapting to fit your use-case, and will likely cause as many problems as it solves if not tweaked and refined to fit your needs.

/**
 * Removes WP cruft
 */class Cruft {

/**
 * Register the integration.
 *
 * @return void
 */public static function register() : void {
$instance = new self();

// Filters.
add_filter( 'wp_print_styles', array( $instance, 'remove_gutenberg_cruft' ), 100 );
add_filter( 'sanitize_file_name', array( $instance, 'force_lowercase_filenames' ), 10, 1 );
add_filter( 'should_load_separate_core_block_assets', '__return_true' ); // Optimize block resource loading.

// Chunkier functions.
$instance->remove_emoji_cruft();
$instance->remove_head_cruft();
$instance->disable_xmlrpc();
}

/**
 * Disable XMLRPC functionality
 *
 * @return void
 */private function disable_xmlrpc() : void {

// Disable on the back end.
add_filter( 'xmlrpc_enabled', '__return_false' );
add_filter( 'pre_option_enable_xmlrpc', '__return_false' );

// Disable the methods.
add_filter(
'xmlrpc_methods',
function( $methods ) {
unset( $methods['pingback.ping'] );
unset( $methods['pingback.extensions.getPingbacks'] );
return $methods;
}
);

// Remove the header link.
add_filter(
'bloginfo_url',
function( $output, $property ) {
return ( $property == 'pingback_url' ) ? '#' : $output;
},
11,
2
);

// Remove the HTTP header.
add_filter(
'wp',
function() {
header_remove( 'X-Pingback' );
},
9999
);

}

/**
 * Remove Gutenberg CSS
 *
 * @return void
 */public function remove_gutenberg_cruft() : void {
wp_deregister_style( 'wp-block-library' );
wp_deregister_style( 'wp-block-library-theme' );
wp_deregister_style( 'wp-block-library-css' );
wp_deregister_style( 'wp-block-paragraph' );
wp_deregister_style( 'classic-theme-styles' );
wp_deregister_style( 'global-styles' );
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
wp_dequeue_style( 'wp-block-library-css' );
wp_dequeue_style( 'classic-theme-styles' );
wp_dequeue_style( 'wp-block-paragraph' );
wp_dequeue_style( 'global-styles' );
}

/**
 * Renames uploaded media to use lowercase URLs
 *
 * @param  string $filename The filename.
 *
 * @return string           The modified filename
 */public function force_lowercase_filenames( $filename ) {
$info = pathinfo( $filename );
$ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
return strtolower( $name ) . $ext;
}

/**
 * Remove inline emoji scripts & styles
 *
 * @return void
 */private function remove_emoji_cruft() : void {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
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( 'wp_resource_hints', array( $this, 'remove_emoji_dns_prefetch' ), 10, 2 );
}

/**
 * Removes some unused DNS prefetch hints
 *
 * @param  array  $hints         The hints.
 *
 * @param  string $relation_type The type of hint.
 *
 * @return array                 The modified hints
 */public function remove_emoji_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
$matches = preg_grep( '/emoji/', $hints );
return array_diff( $hints, $matches );
}
return $hints;
}

/**
 * Disables pingback headers
 *
 * @param  array $headers  The headers.
 *
 * @return array           The modified headers
 */public function remove_x_pingback( $headers ) {
unset( $headers['X-Pingback'] );
return $headers;
}

/**
 * Removes cruft from the <head>
 *
 * @return void
 */private function remove_head_cruft() : void {
remove_action( 'wp', 'rsd_link', 9 );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
add_filter( 'wp_headers', array( $this, 'remove_x_pingback' ), 10, 1 );
}

}

0 Comments
Inline Feedbacks
View all comments