Last active
February 2, 2022 15:39
-
-
Save metaylimpo/201c3964390d362f39cfc59feb1916d3 to your computer and use it in GitHub Desktop.
Example of wordpress theme function file for several optimizations
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
/* | |
DO NOT COPY\PASTE. Take only what you need carefully. | |
The following code is just a few **examples** of optimizations that you can do in functions.php file. | |
*/ | |
<?php | |
// disable google fonts | |
add_filter( 'elementor/frontend/print_google_fonts', '__return_false' ); | |
// disable eicons | |
add_action( 'elementor/frontend/after_enqueue_styles', 'js_dequeue_eicons' ); | |
function js_dequeue_eicons() { | |
// Don't remove it in the backend | |
if ( is_admin() || current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
wp_dequeue_style( 'elementor-icons' ); | |
} | |
add_action( 'elementor/frontend/after_register_styles',function() { | |
foreach( [ 'solid', 'regular', 'brands' ] as $style ) { | |
wp_deregister_style( 'elementor-icons-fa-' . $style ); | |
} | |
}, 20 ); | |
add_action( 'wp_enqueue_scripts', 'replace_font_awesome', 3 ); | |
add_action( 'elementor/frontend/after_enqueue_styles', function () { wp_dequeue_style( 'font-awesome' ); } ); | |
function replace_font_awesome() { | |
wp_enqueue_style( 'font-awesome', '../../fonts/swift-performance/fontawesome/webfont.css' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'remove_default_stylesheet', 20 ); | |
function remove_default_stylesheet() { | |
wp_deregister_style( 'elementor-icons' ); | |
} | |
function add_favicon() { | |
echo '<link rel="shortcut icon" type="image/webp" href="data:image/webp;base64,UklGRnID...........AAAAAAAAAAAA="/>'; | |
} | |
add_action('wp_head', 'add_favicon'); | |
/*remove query string */ | |
function _remove_script_version( $src ){ | |
$parts = explode( '?ver', $src ); | |
return $parts[0]; | |
} | |
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 ); | |
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 ); | |
/* disable feeds and remove them from head */ | |
function itsme_disable_feed() { | |
wp_die(__('No feed available, please visit the <a href="' . esc_url(home_url('/')) . '">homepage</a>!')); | |
} | |
add_action('do_feed', 'itsme_disable_feed', 1); | |
add_action('do_feed_rdf', 'itsme_disable_feed', 1); | |
add_action('do_feed_rss', 'itsme_disable_feed', 1); | |
add_action('do_feed_rss2', 'itsme_disable_feed', 1); | |
add_action('do_feed_atom', 'itsme_disable_feed', 1); | |
add_action('do_feed_rss2_comments', 'itsme_disable_feed', 1); | |
add_action('do_feed_atom_comments', 'itsme_disable_feed', 1); | |
remove_action( 'wp_head', 'feed_links_extra', 3 ); | |
remove_action( 'wp_head', 'feed_links', 2 ); | |
/*RSD is a discovery service that helps discover Pingbacks and XML-RPC on WordPress blogs*/ | |
remove_action( 'wp_head', 'rsd_link' ); | |
/* remove version from head */ | |
remove_action('wp_head', 'wp_generator'); | |
// remove version from rss | |
add_filter('the_generator', '__return_empty_string'); | |
// remove version from scripts and styles | |
function remove_version_scripts_styles ($src) { | |
if (strpos($src, 'ver=')) { | |
$src = remove_query_arg('ver', $src); | |
} | |
return $src; | |
} | |
add_filter('style_loader_src', 'remove_version_scripts_styles', 9999); | |
add_filter('script_loader_src', 'remove_version_scripts_styles', 9999); | |
if (!is_admin()) { | |
add_filter( 'script_loader_tag', function ( $tag, $handle ) { | |
if ( strpos( $tag, "jquery.js" ) || | |
strpos( $tag, "other tags that you want") ) { | |
return $tag; | |
} | |
return str_replace( ' src', ' defer="defer" src', $tag ); | |
}, 10, 2 ); | |
} | |
// Windows Live Writer | |
remove_action('wp_head', 'wlwmanifest_link'); | |
// Remove short link | |
add_filter('after_setup_theme', 'remove_redundant_shortlink'); | |
function remove_redundant_shortlink() { | |
// remove HTML meta tag | |
// <link rel='shortlink' rel="noopener" href='http://example.com/?p=25' /> | |
remove_action('wp_head', 'wp_shortlink_wp_head', 10); | |
// remove HTTP header | |
// Link: <https://example.com/?p=25>; rel=shortlink | |
remove_action( 'template_redirect', 'wp_shortlink_header', 11); | |
} | |
// Disable self pingback | |
function no_self_ping( &$links ) { | |
$home = get_option( 'home' ); | |
foreach ( $links as $l => $link ) | |
if ( 0 === strpos( $link, $home ) ) | |
unset($links[$l]); | |
} | |
add_action( 'pre_ping', 'no_self_ping' ); | |
// remove rest API | |
add_action('after_setup_theme', function(){ | |
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); | |
}); | |
// remove dashicons | |
add_action( 'wp_enqueue_scripts', 'deregister_dashicon' ); | |
function deregister_dashicon() { | |
if (current_user_can( 'update_core' )) { | |
return; | |
} | |
wp_deregister_style('dashicons'); | |
} | |
/*Enable upload for webp image files*/ | |
function webp_upload_mimes($existing_mimes) { | |
$existing_mimes['webp'] = 'image/webp'; | |
return $existing_mimes; | |
} | |
add_filter('mime_types', 'webp_upload_mimes'); | |
/* Enable preview / thumbnail for webp image files*/ | |
function webp_is_displayable($result, $path) { | |
if ($result === false) { | |
$displayable_image_types = array( IMAGETYPE_WEBP ); | |
$info = @getimagesize( $path ); | |
if (empty($info)) { | |
$result = false; | |
} elseif (!in_array($info[2], $displayable_image_types)) { | |
$result = false; | |
} else { | |
$result = true; | |
} | |
} | |
return $result; | |
} | |
add_filter('file_is_displayable_image', 'webp_is_displayable', 10, 2); | |
function preconnect() { | |
echo '<link rel="preconnect" href="//www.google-analytics.com" crossorigin>'; | |
} | |
add_action( 'wp_head', 'preconnect' ); | |
// Adding DNS Prefetching | |
function dns_prefetch() { | |
echo '<meta https-equiv="x-dns-prefetch-control" content="on"> | |
<link rel="dns-prefetch" href="//fonts.googleapis.com" /> | |
<link rel="dns-prefetch" href="//fonts.gstatic.com" /> | |
<link rel="dns-prefetch" href="//0.gravatar.com/" /> | |
<link rel="dns-prefetch" href="//2.gravatar.com/" /> | |
<link rel="dns-prefetch" href="//1.gravatar.com/" /> | |
<link rel="dns-prefetch" href="//maps.googleapis.com/" /> | |
<link rel="dns-prefetch" href="//maps.google.com/" /> | |
<link rel="dns-prefetch" href="//maps.gstatic.com/" /> | |
<link rel="dns-prefetch" href="//disqus.com/" /> | |
<link rel="dns-prefetch" href="//go.disqus.com/" />'; | |
} | |
add_action('wp_head', 'dns_prefetch', 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment