Skip to content

Instantly share code, notes, and snippets.

@ivo-ivanov
Last active April 1, 2024 11:23
Show Gist options
  • Save ivo-ivanov/7ff88a3fd91cb3b31e1b366358a6c002 to your computer and use it in GitHub Desktop.
Save ivo-ivanov/7ff88a3fd91cb3b31e1b366358a6c002 to your computer and use it in GitHub Desktop.
Optimize WordPress. Remove unnecessary code from wp_head. Disable trackbacks and pings. Disable and remove comments on front-end and back-end. Remove oEmbed functionality. Disable emojis on front-end and remove the tinymce emoji plugin. Remove link tags in header. Remove jQuery Migrate #wordpress
<?php
// Remove Unnecessary Code from wp_head
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action( 'wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link');
remove_action( 'wp_head', 'feed_links', 2 );
//Remove JQuery migrate
function remove_jquery_migrate($scripts)
{
if (!is_admin() && isset($scripts->registered['jquery'])) {
$script = $scripts->registered['jquery'];
if ($script->deps) { // Check whether the script has any dependencies
$script->deps = array_diff($script->deps, array(
'jquery-migrate'
));
}
}
}
add_action('wp_default_scripts', 'remove_jquery_migrate');
// Remove oEmbed
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
// Disable Trackbacks and Pings
add_action( 'pre_ping', 'c45_internal_pingbacks' );
add_filter( 'wp_headers', 'c45_x_pingback');
add_filter( 'bloginfo_url', 'c45_pingback_url') ;
add_filter( 'bloginfo', 'c45_pingback_url') ;
add_filter( 'xmlrpc_enabled', '__return_false' );
add_filter( 'xmlrpc_methods', 'c45_xmlrpc_methods' );
// Disable internal pingbacks
function c45_internal_pingbacks( &$links ) {
foreach ( $links as $l => $link ) {
if ( 0 === strpos( $link, get_option( 'home' ) ) ) {
unset( $links[$l] );
}
}
}
// Disable x-pingback
function c45_x_pingback( $headers ) {
unset( $headers['X-Pingback'] );
return $headers;
}
// Remove pingback URLs
function c45_pingback_url( $output, $show='') {
if ( $show == 'pingback_url' ) $output = '';
return $output;
}
// Disable XML-RPC methods
function c45_xmlrpc_methods( $methods ) {
unset( $methods['pingback.ping'] );
return $methods;
}
// Disable and Remove Comments functions
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
// Disable the emoji's
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
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( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'disable_emojis' );
// Filter function used to remove the tinymce emoji plugin.
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
// Remove emoji CDN hostname from DNS prefetching hints.
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' == $relation_type ) {
/** This filter is documented in wp-includes/formatting.php */
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}
// Clean up output of stylesheet <link> tags
function clean_style_tag($input) {
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
if (empty($matches[2])) {
return $input;
}
// Only display media if it is meaningful
$media = $matches[3][0] !== '' && $matches[3][0] !== 'all' ? ' media="' . $matches[3][0] . '"' : '';
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
}
add_filter('style_loader_tag', 'clean_style_tag');
// Include optimize-wp.php in functions.php (optional)
if( file_exists( get_theme_file_path("/inc/optimize-wp.php") ) ) {
include( get_theme_file_path("/inc/optimize-wp.php") );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment