How to REALLY clean up WordPress

WordPress is the most used CMS when it comes to building websites today. It’s a CMS(content management system) we’ve all had at least a little experience using but as with any CMS, there are things about it you wish you could change from the start.

WordPress was originally designed to be a blogging platform and still serves this function. Today, many more developers are using it to do more by adding their own customizations which include custom post types, taxonomies, functions, and more. You can even now access a WordPress REST API and just use WordPress as an authoring platform bypassing all the included functions, filters, and loops.

Like me, you may use it just as a CMS and not always for a blog for the websites you’re building. Doing this means you don’t require all the bells and whistles included from the start. From here you would need to go through all the theme files and delete/optimize functions and features to suit your needs. If you build a lot of websites this can get old fast.

Assessing the problem

I recently started building a new website with WordPress as the CMS. I realized when kicking things off I tend to repeat a lot of steps to setup and optimized WordPress to work better for me as opposed to just diving straight into theme development.

When viewing source on the latest theme bundled with WordPress I found quite a bit of unnecessary requests going on as well as the addition of some libraries I’ll never make use of for my given installation. I also peaked around the CMS itself to uncover a lot of unnecessary things I’ll never make use of.

These include:

  • Emoji support
  • RSS Feed (Unless I need the blog)
  • RSS Comments Feeds (Unless I need comments on the blog)
  • Comments (Unless I need the blog)
  • Versioning (cool feature but don’t always require it. This eats up resources as well)

I think you get the idea… The list goes on but it will always vary from website to website.

How do I clean WordPress?

With a little searching and trial and error, I’ve come up with a few hacks to clean things up and make WordPress as well as my theme optimized for my workflow.

It starts with your theme

You can certainly go about kicking off your website development using a child theme based on the included themes. This is particularly useful for updates but I personally like full control of my themes.

Feel free to skip this part and just download a theme directly from http://underscores.me/.

If you’re new to Gulp you can read more about it here. The same applies for Sass.

  • With my theme all setup and up and running I can start to optimize it further with my own customizations not already provided by the underscores based theme.

Removing Emoji

I think emoji is something you either love or hate. I personally don’t make use of it on my sites so it’s a shame is automatically comes bundled in the WordPress download. Luckily, there’s an action for removing the Emoji WordPress forces on you. You’ll need to edit your functions.php file or add a plugin.

// REMOVE EMOJI ICONS
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

Entering the code above inside your functions.php file now removes the script and styles that get injected into your theme.

Cleaning up the wp_head

Cleaning up the wp_head is probably the biggest enhancement you can make when it comes to cleaning up your website housed on WordPress. There are a number of ways to go about this.

Remove unnecessary feeds and manifests

If you don’t require any RSS feeds you can add this action to your functions.php file.

add_action( 'after_theme_support', 'remove_feed' );

function remove_feed() {
   remove_theme_support( 'automatic-feed-links' )
}

Extras
Below are extra actions you can remove to clean up what gets added to the wp_head by default. Some of this is certainly useful to keep if you’re building a blog based website. Your mileage may vary so use with caution:

// Use what works best for your website
add_action('init', ‘my_head_cleanup');

function my_head_cleanup() {
  // Category Feeds
  remove_action( 'wp_head', 'feed_links_extra', 3 ); 
  
  // Post and Comment Feeds
  remove_action( 'wp_head', 'feed_links', 2 );
  
  // EditURI link
  remove_action( 'wp_head','rsd_link' );                                 

  // Windows Live Writer
  remove_action( 'wp_head','wlwmanifest_link' );                         
  
  // index link  
  remove_action( 'wp_head','index_rel_link' );                           
  
  // previous link
  remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );              
        
  // start link
  remove_action( 'wp_head', 'start_post_rel_link', 10,0 );          
  
  // Links for Adjacent Posts
  remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );   

  // WP version
   remove_action( 'wp_head', 'wp_generator' );   
}

How to remove comment support

If you simply don’t need the commenting functionality built into WordPress this would be your best bet as to removing it. WordPress comments are great and all but are highly prone to attacks from bots and spam.

I recently dropped support for the native comments entirely and switched to Disqus. While not completely ideal, it has lifted the headache of deleting so much spam on this site:

<?php
// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );
function my_remove_admin_menus() {
    remove_menu_page( 'edit-comments.php' );
}
// Removes from post and pages
add_action('init', 'remove_comment_support', 100);

function remove_comment_support() {
    remove_post_type_support( 'post', 'comments' );
    remove_post_type_support( 'page', 'comments' );
}
// Removes from admin bar
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
?>

Reference URL

Concatenate Your Scripts and Stylesheets

You may see a ton of links to different libraries and stylesheets associated with various plugins you’ve installed. While everything is working, your source code is probably an eyesore not to mentioned your page speed.

To help with this you can opt to concatenate your scripts and styles from all areas of the site to create one global file for your website to references as opposed to many of them.

The best way to go about this is a plugin. There are a few available but I’ve had the best luck with MinQueue. You can choose what to enqueue from the front end of the site on a per file basis. This gives you full control which is wonderful since most plugins are highly opinionated.

Chris wrote a nice article on how to handle the CSS and JavaScript WordPress plugin load which you can refer to here.

Cleaning up the database

If you get a lot of comments or have a lot of post/page revisions you’ll want to be on top of your database with either a plugin or disabling those features completely. Depending on the website you can decide what your best options are and plan for them from the start.

Removing post revisions
If you want to remove post revision completely you can disable them by adding this action to your root wp-config.php file:

define('WP_POST_REVISIONS', false);

If you decide you want to keep revisions there’s actually a way to limit the number of revisions to keep. To do that add this to your wp-config.php file inside of the previous code.

// Add an integer between the `<`,’>’ signs.
define('WP_POST_REVISIONS', < number of revisions >);

Delete unnecessary plugins

WordPress plugins include their own styles and scripts which create additional requests. As a result, your site slows down and becomes annoying for your users. To removing the taxing qualities these hold on your server you can delete unnecessary plugins.

Sometimes when you delete plugins your database is left with useless tables filled with data from those plugins. You’ll either want to remove those manually or use a plugin likeWP Optimize to clean your database.

Conclusion

While this is only the tip of the iceberg your results will vary for each and every website you build. Over time, I imagine you’ll develop a process which automates most of this. If you’re feeling froggy maybe you can even create your own starter theme or WordPress yeoman generator to handle these tasks for you from the start.