Skip to content

Instantly share code, notes, and snippets.

@hdtvspace
Last active April 15, 2017 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdtvspace/258c36dc0d5f607c1a215fea1d6c9a77 to your computer and use it in GitHub Desktop.
Save hdtvspace/258c36dc0d5f607c1a215fea1d6c9a77 to your computer and use it in GitHub Desktop.
WordPress Tuning, jquery for defer loading and async loading of css files. But not for admin. It breaks then the wp editor.
// remove all this Worpress nonsens for tuning and better performance.
add_filter('xmlrpc_enabled', '__return_false' );
add_filter('got_rewrite', '__return_true', 999);
add_filter('wp_get_attachment_image_attributes', 'ipwp_img_attr', 10, 2); function ipwp_img_attr($attr) { $attr['itemprop'] = 'image'; return $attr;}
add_filter('xmlrpc_methods', function($methods) { unset($methods['wp.getUsersBlogs']); return $methods; });
add_filter('wp_headers', 'FastWP_remove_x_pingback' ); function FastWP_remove_x_pingback( $headers ) { unset( $headers['X-Pingback'] ); return $headers;}
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_action( 'init', 'disable_emojis' );
function disable_emojis_tinymce( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } else {return array(); } }
add_action('init', 'remheadlink'); function remheadlink() {remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('wp_head', 'wp_shortlink_header', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_resource_hints', 2 );}
// disable it
add_filter( 'emoji_svg_url', '__return_false' );
function block_wp_embed() {wp_deregister_script('wp-embed'); }
add_action('init', 'block_wp_embed');
add_action('init', 'stop_heartbeat', 1);
function stop_heartbeat() {wp_deregister_script('heartbeat');}
remove_filter( 'the_content', 'wp_make_content_images_responsive' );
function reassign_jQuery() {
wp_deregister_script( 'jquery' );
wp_deregister_script( 'jquery-core' );
wp_deregister_script( 'jquery-migrate' );
wp_register_script('jquery', get_stylesheet_directory_uri() . '/js/jquery.js', array(), null, true );
wp_enqueue_script('jquery');}
if ( ! is_admin() ) add_action('init', 'reassign_jQuery');
// https://github.com/filamentgroup/loadCSS
if ( ! is_admin() ) add_action('wp_head','add_load_css',7);
function add_load_css(){ ?><script><?php readfile(get_stylesheet_directory() . '/js/loadCSS.js'); ?></script><?php }
if ( ! is_admin() ) add_filter('style_loader_tag', 'link_to_loadCSS_script',9999,3);
function link_to_loadCSS_script($html, $handle, $href ) {
$dom = new DOMDocument();
$dom->loadHTML($html);
$a = $dom->getElementById($handle.'-css');
return "<script>loadCSS('" . $a->getAttribute('href') . "',0,'" . $a->getAttribute('media') . "');</script>\n";
}
// function to add defer or async to all scripts
function js_async_attr($tag){ return str_replace( ' src', ' defer="defer" src', $tag ); }
if ( ! is_admin() ) add_filter( 'script_loader_tag', 'js_async_attr', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment