Created
November 15, 2018 06:52
-
-
Save alexwcoleman/8539c684aaf86a428dcb381c3ea17911 to your computer and use it in GitHub Desktop.
WordPress Cleanup
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 | |
// ALL OF THIS IS FROM: https://www.linkedin.com/pulse/wp-speed-up-from-ground-alex-knopp | |
// Post Revisions | |
// WordPress will simply store every single post revision you create. If your a developer like me then my clients websites change a lot and these post revisions build up. Add the following to your wp-config.php file to stipulate the number of post revisions saved at any one time. | |
//Define the number of post revisions to be saved | |
define( 'WP_POST_REVISIONS', 3 ); | |
// WordPress Bloat | |
// If your coding your own themes and plugins then your probably already aware that WordPress adds its own code here and there and begins to bloat out areas in the dom that arent required. For example, menu classes and ID's and body classes. | |
// > Clean up the navigation classes and id's | |
// Remove the ID from the menu item | |
function clear_nav_menu_item_id($id, $item, $args) { | |
return ""; | |
} | |
add_filter('nav_menu_item_id', 'clear_nav_menu_item_id', 10, 3); | |
// Remove the class from the menu item | |
function clear_nav_menu_item_class($classes, $item, $args) { | |
return array(); | |
} | |
add_filter('nav_menu_css_class', 'clear_nav_menu_item_class', 10, 3); | |
// > Dequeue jQuery Migrate | |
function dequeue_jquery_migrate( &$scripts){ | |
if(!is_admin()){ | |
$scripts->remove( 'jquery'); | |
$scripts->add( 'jquery', false, array( 'jquery-core' ), '1.10.2' ); | |
} | |
} | |
add_filter( 'wp_default_scripts', 'dequeue_jquery_migrate' ); | |
// > Pesky header code bloat such as RSS links, version generator, post links etc | |
function removeHeadLinks() { | |
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Display the links to the extra feeds such as category feeds | |
remove_action( 'wp_head', 'feed_links', 2 ); // Display the links to the general feeds: Post and Comment Feed | |
remove_action( 'wp_head', 'rsd_link' ); // Display the link to the Really Simple Discovery service endpoint, EditURI link | |
remove_action( 'wp_head', 'wlwmanifest_link' ); // Display the link to the Windows Live Writer manifest file. | |
remove_action( 'wp_head', 'index_rel_link' ); // index link | |
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // prev link | |
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link | |
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // Display relational links for the posts adjacent to the current post. | |
remove_action( 'wp_head', 'wp_generator' ); // Display the XHTML generator that is generated on the wp_head hook, WP version | |
} | |
add_action('init', 'removeHeadLinks'); | |
// > Disable XMLRPC. More of a security feature than a performance one. | |
add_filter('xmlrpc_enabled', '__return_false'); | |
// > If your not linkely to be using comments then you wont be using emjois. | |
function disable_wp_emojicons() { | |
// all actions related to emojis | |
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' ); | |
// filter to remove TinyMCE emojis | |
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' ); | |
} | |
add_action( 'init', 'disable_wp_emojicons' ); | |
function disable_emojicons_tinymce( $plugins ) { | |
if ( is_array( $plugins ) ) { | |
return array_diff( $plugins, array( 'wpemoji' ) ); | |
} else { | |
return array(); | |
} | |
} | |
add_filter( 'emoji_svg_url', '__return_false' ); | |
// HTML Minify | |
// You can minify front end code like HTML, CSS and JS with the plugins Ive mentioned above however, you can also do this as part of your own theme development using the following I found on github. Sorry to the author I just cant find you again. | |
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 '<!-- HTML Minify | Gross page reduction of '.$savings.'% | From '.$raw.' Bytes, To '.$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'); | |
// Clean up the Admin Menu | |
// Whilst this doesn't really help the front end or user experience, it will help you keep your admin view more organised. There are plugins available fo this however, I prefer this route as it can be more flexible when it comes to views not recognised or included in the plugins available. | |
// This will help you retain all your permissions but remove the menu items from the admin. Helpfull if your client needs admin rights but, you want to keep them away from things they either dont need or are likely to break. | |
function remove_menus(){ | |
remove_menu_page( 'index.php' ); //Dashboard | |
remove_menu_page( 'edit-comments.php' ); | |
remove_menu_page( 'themes.php' ); | |
remove_menu_page( 'plugins.php' ); | |
remove_menu_page( 'tools.php' ); | |
remove_menu_page( 'options-general.php' ); | |
remove_menu_page( 'edit.php?post_type=acf' ); //ACF Custom Fields | |
} | |
add_action( 'admin_menu', 'remove_menus' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment