Get DevWP - WordPress Development Theme

Speed Up Your WordPress Website By Minifying your Code

WordPress is a powerful CMS that powers the vast majority of the Modern Web. It’s a dynamic platform that makes it amazingly easy to share your thoughts, ideas, opinions and in the case of business, your services & products.

The fact that WordPress is a dynamic CMS means it uses some powerful coding languages to make it function the way we want it too. Sometimes it can be easy to just take it for granted, but there’s a lot of work going on Under the hood of WordPress, (did you read that article yet?) The languages involved are the same coding languages used to power some of the most popular websites on the web, Facebook being one of them.

  • HTML
  • CSS
  • JavaScript
  • PHP
  • MySQL

With these coding languages, you have the power to create a truly amazing/powerful website. But with that power comes great responsibility. You’re responsible for delivering your content to your website visitors as fast as possible.

While there are a lot of ways to increase your WordPress Website’s performance including the use of plugins, sometimes it’s a great option to just use plain old fashion snippets of code.

I created a YouTube Video demonstrating how to add this code to your theme files but I also wanted to share the code with you so you can copy and paste it which is a lot easier than having to make sure you type it out exactly as shown in a video. The YouTube video is down below but first let’s look at the code.

Note: this code might not work well with all themes. Be mindful that code conflicts can exist with either the theme you’re using or any mix of plugins you might have installed. I recommend testing this out locally first with your same setup on a local version of your website. I mean everything from your theme and plugins used so you can be sure it will play nice with your site.


if ( !is_user_logged_in() ) {
function sanitize_output($buffer) {
 $search = array(
 '/\>[^\S ]+/s', // strip whitespaces after tags, except space
 '/[^\S ]+\',
 '< ',
 '\\1'
 );
 $buffer = preg_replace($search, $replace, $buffer);
 return $buffer;
}
ob_start("sanitize_output");
}
  • The code above starts off with an if conditional statement that checks if the user is logged in. This is an important part of the code because if the user is logged in, the code won’t be minified. This will make it easier to inspect the code using your developer tools from your favorite web browser. Note the closing tag is at the end of the code snippet.
  • The code proceeds to creating a function named sanitize_output and inside the parenthesis is the variable $buffer.
  • Inside the second set of curly braces are three variables; $search, $replace and $buffer.
  • Inside the $search variable array is literally searching for whitespace before and after tags and also shortens multiple whitespace sequences. Then it closes out the array with the semi-colon.
  • The second variable $replace array is setting the stage for the final stage by setting what it will replace from what’s found by the $search variable.
  • The final variable $buffer is using the preg_replace regex function and inside the parenthesis are the 3 variables.
  • We then return the $buffer variable with the results.
  • We then use the ob_start function with the sanitize_output function.

The results are that if a person isn’t logged in, they will be delivered a much faster web page since the html will be minified. It’s important to make sure you insert the code properly, either in your themes functions.php file or by using a plugin that enables you to add php code to your website that won’t be deleted when your theme is changed.

Again, make sure you are following the best practices of testing locally first and then once you’re confident that it does indeed play nice with your theme and plugin mix, then deploy to your live website.

Check out the video to get a walk through.



View Our Themes