Skip to content

Instantly share code, notes, and snippets.

@albofish
Created May 11, 2016 13:23
Show Gist options
  • Save albofish/4668eee8f565a4e7178d302cdf7ba494 to your computer and use it in GitHub Desktop.
Save albofish/4668eee8f565a4e7178d302cdf7ba494 to your computer and use it in GitHub Desktop.
Quick Wordpress Optimisations
<?php
define('WP_POST_REVISIONS', 0); // Remove post revision support
add_theme_support( 'post-thumbnails' ); // Add post thumbnail support
add_theme_support('html5', array('comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'widgets')); // Add html5 support
/**
* Clean up Wordpress HTML output
*/
function wp_clean_setup () {
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
add_filter('the_generator', '__return_false');
add_filter('show_admin_bar','__return_false');
remove_action('set_comment_cookies', 'wp_set_comment_cookies');
// emoji be gone
remove_action( 'admin_print_styles', 'print_emoji_styles' );
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_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
}
function disable_emojicons_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
add_action('init', 'wp_clean_setup');
/**
* Nice redirects for attachment templates
*/
function wp_clean_template_redirect () {
global $wp_query, $post;
if ( is_attachment() ) {
$post_parent = $post->post_parent;
if ( $post_parent ) {
wp_redirect( get_permalink($post->post_parent), 301 );
exit;
}
$wp_query->set_404();
return;
}
if ( is_author() || is_date() ) {
$wp_query->set_404();
}
}
add_action( 'template_redirect', 'wp_clean_template_redirect' );
/**
* Responsive video embed and Youtube URL modifications
*/
function wp_clean_embed_oembed_html($html, $url, $attr, $post_id) {
$html = preg_replace('# src="https://www.youtube.com([^"]*)"#', ' src="https://www.youtube-nocookie.com1&rel=0&modestbranding=1"', $html);
return '<div class="video-wrapper">' . $html . '</div>';
}
add_filter('embed_oembed_html', 'wp_clean_embed_oembed_html', 9999, 4);
/**
* Tidy default WP menu HTML
*/
function my_menu_classes_filter($var)
{
return is_array($var) ? array_intersect($var, array('menu-item', 'current-menu-item', 'current-menu-parent')) : '';
}
add_filter('nav_menu_css_class', 'my_menu_classes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_menu_classes_filter', 100, 1);
add_filter('page_css_class', 'my_menu_classes_filter', 100, 1);
/**
* Remove jQuery from front-end (replace with 2.* branch in footer)
*/
function wp_clean_remove_jquery() {
if ( !is_admin() ) {
wp_deregister_script('jquery');
}
}
add_action( 'wp_enqueue_scripts', 'wp_clean_remove_jquery' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment