tamanmerah

functions.php

Jun 7th, 2021 (edited)
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.58 KB | None | 0 0
  1. // conditional loading
  2. function conditionally_load_plugin_js_css(){
  3. wp_deregister_script('heartbeat'); // deregister heart beat
  4. wp_dequeue_script('wp-embed'); // disable embed
  5. wp_dequeue_script('contact-form-7'); // disable contact-form-7
  6. wp_dequeue_script('jquery.timeago.id.js');
  7. wp_dequeue_script('wpo-minify-footer-jquery-timeago-id.min.js');
  8. wp_dequeue_style('snapcode.min.css');
  9. wp_dequeue_style('contact-form-7');
  10. wp_dequeue_style('dashicons'); // disable dash icons
  11. }
  12. add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css' );
  13. // disable xmlrpc
  14. add_filter( 'xmlrpc_enabled', '__return_false' );
  15. // remove wlwmanifest
  16. remove_action('wp_head', 'wlwmanifest_link');
  17. // remove rsd link
  18. remove_action('wp_head', 'rsd_link');
  19. // remove shortlink
  20. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);  
  21. // remove emoticon
  22. 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');
  23. // only for post and page
  24. add_filter( 'postmeta_form_limit', 'limit_postmeta_keys_to_zero', 10, 1 );function limit_postmeta_keys_to_zero( $limit ) {global $post;$valid_post_types = array('page','post');if( is_admin() ): if( in_array( $post->post_type, $valid_post_types ) ) {return 0;}endif;return $limit;}
  25. // remove pings to self
  26. 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');
  27. // unregister all default widgets
  28. function unregister_default_wp_widgets(){unregister_widget('WP_Widget_Pages');unregister_widget('WP_Widget_Calendar');unregister_widget('WP_Widget_Archives');unregister_widget('WP_Widget_Links');unregister_widget('WP_Widget_Meta');unregister_widget('WP_Widget_Search');unregister_widget('WP_Widget_Text');unregister_widget('WP_Widget_Categories');unregister_widget('WP_Widget_Recent_Posts');unregister_widget('WP_Widget_Recent_Comments');unregister_widget('WP_Widget_RSS');unregister_widget('WP_Widget_Tag_Cloud');}add_action('widgets_init','unregister_default_wp_widgets',1);
  29. // remove extra CSS that 'Recent Comments' widget injects
  30. add_action('widgets_init','remove_recent_comments_style');function remove_recent_comments_style(){global $wp_widget_factory;remove_action('wp_head',array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'],'recent_comments_style'));}
  31. // change author slug to superhero
  32. function new_author_base() {global $wp_rewrite;$author_slug = 'superhero';$wp_rewrite->author_base = $author_slug;}add_action('init', 'new_author_base');
  33. // remove query string from static files
  34. 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);
  35. // remove wp version number from scripts and styles
  36. function remove_css_js_version($src){if(strpos($src,'?ver='))$src=remove_query_arg('ver',$src);return $src;}add_filter('style_loader_src','remove_css_js_version',9999);add_filter('script_loader_src','remove_css_js_version',9999);
  37. // minify
  38. class WP_HTML_Compression{protected $compress_css=true;protected $compress_js=true;protected $info_comment=true;protected $remove_comments=true;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 '<!--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;$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;continue;}else if($this->remove_comments){if(!$overriding&&$raw_tag!='textarea'){$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;$content=preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/','$1',$content);$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');
  39. // disable gutenberg editor
  40. add_filter('use_block_editor_for_post', '__return_false', 10);
Add Comment
Please, Sign In to add comment