Created
February 12, 2020 07:38
-
-
Save hmowais/de952b7c78739eed93323542cf620535 to your computer and use it in GitHub Desktop.
Speed Optimization WordPress (Manually)
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 | |
//defer css | |
function add_rel_preload($html, $handle, $href, $media) { | |
if (is_admin()) | |
return $html; | |
$html = <<<EOT | |
<link defer="defer" rel='stylesheet' href='$href' media="print" onload="this.media='all'" id='$handle' crossorigin="anonymous"/> | |
<noscript><link defer rel="preload" href="$href" crossorigin="anonymous"></noscript> | |
EOT; | |
return $html; | |
} | |
add_filter( 'style_loader_tag', 'add_rel_preload', 10, 4 ); | |
add_action('wp_head', 'hook_css'); | |
//wphead cleanup | |
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 (make sure you add them in yourself if youre using feedblitz or an rss service) | |
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' ); | |
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', 1000 ); | |
add_filter( 'script_loader_src', 'remove_cssjs_ver', 1000 ); | |
// Defer scripts | |
function mind_defer_scripts( $tag, $handle, $src ) { | |
$defer = array( | |
'custom-animate', | |
'owl-carousel', | |
'swiper-jquery', | |
'jquery-migrate', | |
'florida-custom', | |
// 'custom_script', | |
'scrolldepth', | |
'tp-tools', | |
'revmin', | |
'jquery', | |
// 'doubletab', | |
'hoverIntent', | |
'megamenu', | |
'jquery-lazyloadxt', | |
'jquery-lazyloadxt-srcset', | |
'jquery-lazyloadxt-extend', | |
// 'wpb_composer_front_js', | |
// 'vc_accordion_script' | |
'vc_carousel_js', | |
'vc_tabs_script', | |
'vc_tta_autoplay_script', | |
'vc_transition_bootstrap_js', | |
'mediaelement', | |
'contact-form-7', | |
'icegram_main_js', | |
'dynamic-js', | |
'wpcf7-redirect-script' | |
// 'mediaelement-core' | |
); | |
if ( in_array( $handle, $defer ) ) { | |
return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n"; | |
} | |
return $tag; | |
} | |
add_filter( 'script_loader_tag', 'mind_defer_scripts', 10, 3 ); | |
// function PREFIX_remove_scripts() { | |
// wp_dequeue_style( 'dashicons' ); | |
// // wp_dequeue_style( 'layerslider' ); | |
// wp_dequeue_style( 'ls-user' ); | |
// wp_dequeue_style( 'wp-block-library' ); | |
// wp_dequeue_style( 'ls-google-fonts' ); | |
// // wp_dequeue_style( 'layerslider-front' ); | |
// wp_dequeue_style( 'contact-form-7' ); | |
// wp_dequeue_style( 'rs-plugin-settings' ); | |
// wp_dequeue_style( 'gfont-style' ); | |
// wp_dequeue_style( 'flexslider' ); | |
// wp_dequeue_style( 'swiper-style' ); | |
// wp_dequeue_style( 'owl-theme-default' ); | |
// // wp_dequeue_style( 'js_composer_front' ); | |
// wp_dequeue_style( 'megamenu' ); | |
// wp_dequeue_style( 'admin-bar' ); | |
// wp_dequeue_style( 'yoast-seo-adminbar' ); | |
// wp_dequeue_script( 'dynamic-js' ); | |
// wp_dequeue_script( 'jquery' ); | |
// wp_dequeue_script( 'tp-tools' ); | |
// wp_dequeue_script( 'revmin' ); | |
// wp_dequeue_script( 'custom-animate' ); | |
// wp_dequeue_script( 'owl-carousel' ); | |
// wp_dequeue_script( 'swiper-jquery' ); | |
// wp_dequeue_script( 'jquery-migrate' ); | |
// // wp_dequeue_script( 'florida-custom' ); | |
// wp_dequeue_script( 'custom_script' ); | |
// wp_dequeue_script( 'scrolldepth' ); | |
// // wp_dequeue_script( 'doubletab' ); | |
// wp_dequeue_script( 'hoverIntent' ); | |
// wp_dequeue_script( 'megamenu' ); | |
// wp_dequeue_script( 'wpb_composer_front_js' ); | |
// wp_dequeue_script( 'vc_accordion_script' ); | |
// wp_dequeue_script( 'vc_carousel_js' ); | |
// wp_dequeue_script( 'vc_tabs_script' ); | |
// wp_dequeue_script( 'vc_tta_autoplay_script' ); | |
// wp_dequeue_script( 'vc_transition_bootstrap_js' ); | |
// wp_dequeue_script( 'contact-form-7' ); | |
// wp_dequeue_script( 'mediaelement-core' ); | |
// wp_dequeue_script( 'mediaelement' ); | |
// wp_dequeue_script( 'wpcf7-redirect-script' ); | |
// wp_dequeue_script( 'jquery-lazyloadxt' ); | |
// wp_dequeue_script( 'jquery-lazyloadxt-srcset' ); | |
// wp_dequeue_script( 'icegram_main_js' ); | |
// wp_dequeue_script( 'jquery-lazyloadxt-extend' ); | |
// wp_dequeue_script( 'another-jquery' ); | |
// // Now register your styles and scripts here | |
// } | |
// add_action( 'wp_enqueue_scripts', 'PREFIX_remove_scripts', 20 ); | |
// function prefix_add_footer() { | |
// wp_enqueue_style( 'dashicons'); | |
// // wp_enqueue_style( 'layerslider'); | |
// wp_enqueue_style( 'ls-user'); | |
// wp_enqueue_style( 'wp-block-library'); | |
// wp_enqueue_style( 'ls-google-fonts'); | |
// // wp_enqueue_style( 'layerslider-front'); | |
// wp_enqueue_style( 'contact-form-7'); | |
// wp_enqueue_style( 'rs-plugin-settings'); | |
// wp_enqueue_style( 'gfont-style'); | |
// wp_enqueue_style( 'flexslider'); | |
// wp_enqueue_style( 'swiper-style'); | |
// wp_enqueue_style( 'owl-theme-default'); | |
// // wp_enqueue_style( 'js_composer_front'); | |
// wp_enqueue_style( 'megamenu'); | |
// wp_enqueue_style( 'admin-bar'); | |
// wp_enqueue_style( 'yoast-seo-adminbar'); | |
// wp_enqueue_script( 'dynamic-js'); | |
// wp_enqueue_script( 'jquery' ); | |
// // wp_enqueue_script( 'doubletab' ); | |
// wp_enqueue_script( 'tp-tools' ); | |
// wp_enqueue_script( 'revmin' ); | |
// wp_enqueue_script( 'custom-animate' ); | |
// wp_enqueue_script( 'owl-carousel' ); | |
// wp_enqueue_script( 'swiper-jquery' ); | |
// wp_enqueue_script( 'jquery-migrate' ); | |
// // wp_enqueue_script( 'florida-custom' ); | |
// wp_enqueue_script( 'custom_script' ); | |
// wp_enqueue_script( 'scrolldepth' ); | |
// wp_enqueue_script( 'hoverIntent' ); | |
// wp_enqueue_script( 'megamenu' ); | |
// wp_enqueue_script( 'wpb_composer_front_js' ); | |
// wp_enqueue_script( 'vc_accordion_script' ); | |
// wp_enqueue_script( 'vc_carousel_js' ); | |
// wp_enqueue_script( 'vc_tabs_script' ); | |
// wp_enqueue_script( 'vc_tta_autoplay_script' ); | |
// wp_enqueue_script( 'vc_transition_bootstrap_js' ); | |
// wp_enqueue_script( 'contact-form-7' ); | |
// wp_enqueue_script( 'mediaelement-core' ); | |
// wp_enqueue_script( 'mediaelement' ); | |
// wp_enqueue_script( 'wpcf7-redirect-script' ); | |
// wp_enqueue_script( 'jquery-lazyloadxt' ); | |
// wp_enqueue_script( 'jquery-lazyloadxt-srcset' ); | |
// wp_enqueue_script( 'icegram_main_js' ); | |
// // wp_enqueue_script( 'jquery-lazyloadxt-extend' ); | |
// // wp_enqueue_script( 'another-jquery' ); | |
// }; | |
// add_action( 'get_footer', 'prefix_add_footer' ); | |
// html compressor | |
class WP_HTML_Compression | |
{ | |
// Settings | |
protected $compress_css = true; | |
protected $compress_js = false; | |
protected $info_comment = true; | |
protected $remove_comments = true; | |
// Variables | |
protected $html; | |
public function __construct($html) | |
{ | |
if (!empty($html)) | |
{ | |
$this->parseHTML($html); | |
} | |
} | |
public function __toString() | |
{ | |
return $this->html; | |
} | |
protected function bottomComment($raw, $compressed) | |
{ | |
$raw = strlen($raw); | |
$compressed = strlen($compressed); | |
$savings = ($raw-$compressed) / $raw * 100; | |
$savings = round($savings, 2); | |
return '<!--HTML compressed, size saved '.$savings.'%. From '.$raw.' bytes, now '.$compressed.' bytes-->'; | |
} | |
protected function minifyHTML($html) | |
{ | |
$pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si'; | |
preg_match_all($pattern, $html, $matches, PREG_SET_ORDER); | |
$overriding = false; | |
$raw_tag = false; | |
// Variable reused for output | |
$html = ''; | |
foreach ($matches as $token) | |
{ | |
$tag = (isset($token['tag'])) ? strtolower($token['tag']) : null; | |
$content = $token[0]; | |
if (is_null($tag)) | |
{ | |
if ( !empty($token['script']) ) | |
{ | |
$strip = $this->compress_js; | |
} | |
else if ( !empty($token['style']) ) | |
{ | |
$strip = $this->compress_css; | |
} | |
else if ($content == '<!--wp-html-compression no compression-->') | |
{ | |
$overriding = !$overriding; | |
// Don't print the comment | |
continue; | |
} | |
else if ($this->remove_comments) | |
{ | |
if (!$overriding && $raw_tag != 'textarea') | |
{ | |
// Remove any HTML comments, except MSIE conditional comments | |
$content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content); | |
} | |
} | |
} | |
else | |
{ | |
if ($tag == 'pre' || $tag == 'textarea') | |
{ | |
$raw_tag = $tag; | |
} | |
else if ($tag == '/pre' || $tag == '/textarea') | |
{ | |
$raw_tag = false; | |
} | |
else | |
{ | |
if ($raw_tag || $overriding) | |
{ | |
$strip = false; | |
} | |
else | |
{ | |
$strip = true; | |
// Remove any empty attributes, except: | |
// action, alt, content, src | |
$content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content); | |
// Remove any space before the end of self-closing XHTML tags | |
// JavaScript excluded | |
$content = str_replace(' />', '/>', $content); | |
} | |
} | |
} | |
if ($strip) | |
{ | |
$content = $this->removeWhiteSpace($content); | |
} | |
$html .= $content; | |
} | |
return $html; | |
} | |
public function parseHTML($html) | |
{ | |
$this->html = $this->minifyHTML($html); | |
if ($this->info_comment) | |
{ | |
$this->html .= "\n" . $this->bottomComment($html, $this->html); | |
} | |
} | |
protected function removeWhiteSpace($str) | |
{ | |
$str = str_replace("\t", ' ', $str); | |
$str = str_replace("\n", '', $str); | |
$str = str_replace("\r", '', $str); | |
while (stristr($str, ' ')) | |
{ | |
$str = str_replace(' ', ' ', $str); | |
} | |
return $str; | |
} | |
} | |
function wp_html_compression_finish($html) | |
{ | |
return new WP_HTML_Compression($html); | |
} | |
function wp_html_compression_start() | |
{ | |
ob_start('wp_html_compression_finish'); | |
} | |
add_action('get_header', 'wp_html_compression_start'); | |
// remove_action('wp_head', 'wp_print_scripts'); | |
// remove_action('wp_head', 'wp_print_head_scripts', 9); | |
// remove_action('wp_head', 'wp_enqueue_scripts', 1); | |
// add_action('wp_footer', 'wp_print_scripts', 5); | |
// add_action('wp_footer', 'wp_enqueue_scripts', 5); | |
// add_action('wp_footer', 'wp_print_head_scripts', 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment