WordPress Minify Content

WordPress Minify for content will reduce the size amount of your table wp_posts which contain all the content for your posts. Want to reduce the size of your database or even better improve it’s performance? Just like Google suggests to Minify Javascript and CSS the same should also go for your WordPress database and your posts content.

What is Minify?

Minify is the process in which you basically strip out all whitespace from a files content. Believe it or not white space will add to the size of the file and we all know the larger the file the longer it takes to load. If you are dealing with thousands to millions of page views a month this can make a difference in your page speed and performance.

This tool will take all your WordPress content and do the same, it will take out all the whitespace from your table wp_posts in the post_content column and simply remove all that white space.

Warning

Before using this tool you should make sure you have a backup of your table. You can either download it through phpmyadmin (if you have it) or you can simply make a copy of the table just in case things go wrong. I have been using this tool for myself and even on the site you are looking at for some time and have never had a problem, but if this causes a problem with your site I am not responsible for any reason and especially if you did not make a backup.

Also keep in mind that this is going to make your html code a little more difficult to manage since it will all look like it is in one line. Any previous structure you had made your code with will be removed and it will be one giant page of a single line code.

There is no timeout set on this code so unless you have a huge amount of posts (and I am talking in the 100,000’s) then you should be fine.

But the benefits of this are huge especially when you have a large database with lots of posts.

Here is an example of the code and a link to download the file

$host = 'localhost';
$usr = 'MYSQLUSER';
$pass = 'MYSQLPASS';
$dbh2 = mysql_connect($host, $usr, $pass, true); 

function sanitize_output($buffer) {

 $search = array(
 '/\>[^\S ]+/s', // strip whitespaces after tags, except space
 '/[^\S ]+\</s', // strip whitespaces before tags, except space
 '/(\s)+/s' // shorten multiple whitespace sequences
 );

 $replace = array(
 '>',
 '<',
 '\\1'
 );

 $buffer = preg_replace($search, $replace, $buffer);

 return $buffer;
}

ob_start("sanitize_output");

$sql = "SELECT ID,post_content FROM wp_posts ORDER BY ID DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
 $ID = $row['ID'];
 $html = $row['post_content'];

 $html = sanitize_output($html);

 $sql1 = "UPDATE wp_posts SET post_content = '$html' WHERE ID = '$ID'";
 $result1 = mysql_query($sql1);
}