-2

i need to speed up my Wordpress blog. I searched around the web, but no success. I want to minify or compress my output html code on one(single) line, like Matt Cutt's blog. I tried W3TC, WP Minify and many others, but without result. I need script, plugin, function or something that works.

Thanks in advance.

4

5 回答 5

1

This is really not the best way to speedup your site. If you do it in the template it make the files unreadable and hard to maintain for less than 1% speedup. If you do it with a plugin that process the output, it will slow down the render.

Make sure :

  • You use as few plugins as possible, for example it's much faster to copy tracking code (google analytics or such) in footer.php than using a plugin
  • You have compiled, cleaned, minifyied CSS and JS that is on your server and properly compressed files.
  • You use CDN for all files that are on CDN like JQuery on https://developers.google.com/speed/libraries/devguide
  • Put mod_expire on your server and set expire date for media files far in future with .htaccess . This will prevent browsers from checking if files have changed (all the 200 status code you see in network traffic analysis)
  • Cache content using WP supercache or similar plugin
  • Install APC cache with enough memory (at least 32M for a single WP installation)
于 2012-09-23T18:31:55.867 回答
1

Here is one solution what I give in WordPress.StackExchange.com

https://wordpress.stackexchange.com/a/227896/82023

Generaly, in index.php you can place one code what will compress your HTML using regex. i made and use this on many places and work fine. Is not complete inline but do it's job.

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Manualy compress WP by Ivijan-Stefan Stipic  **/

function compressorCF($str)
{
    // clear HEAD
    $str = preg_replace_callback('/(?=<head(.*?)>)(.*?)(?<=<\/head>)/s',
    function($matches) {
        return preg_replace(array(
            /* Fix HTML */
            '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
            '/[^\S ]+\</s',  // strip whitespaces before tags, except space
            '/\>\s+\</',    // strip whitespaces between tags
        ), array(
            /* Fix HTML */
            '>',  // strip whitespaces after tags, except space
            '<',  // strip whitespaces before tags, except space
            '><',   // strip whitespaces between tags
        ), $matches[2]);
    }, $str);
    // clear BODY
    $str = preg_replace_callback('/(?=<body(.*?)>)(.*?)(?<=<\/body>)/s',
    function($matches) {
        return preg_replace(array(
            '/<!--(.*?)-->/s', // delete HTML comments
            '@\/\*(.*?)\*\/@s', // delete JavaScript comments
            /* Fix HTML */
            '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
            '/[^\S ]+\</s',  // strip whitespaces before tags, except space
            '/\>\s+\</',    // strip whitespaces between tags
        ), array(
            '', // delete HTML comments
            '', // delete JavaScript comments
            /* Fix HTML */
            '>',  // strip whitespaces after tags, except space
            '<',  // strip whitespaces before tags, except space
            '><',   // strip whitespaces between tags
        ), $matches[2]);
    }, $str);
    return $str;
}

/** Loads the WordPress Environment and Template */
ob_start();
    require_once( dirname( __FILE__ ) . '/wp-blog-header.php' );
$content=ob_get_clean();
//echo $content;
echo compressorCF($content); 
于 2016-12-14T17:31:16.220 回答
0

If your plugin loads CSS and JS files properly there is no point to say that they will make your website slow. 1. Another issue is how many database call these plugin make to render a particular task. 2. Whether that plugin check data from a remote server to update something like akismat and jetpack, thus makes these two pluin the most resource hungry.

  1. Proper coded theme can help you to load your website properly. like my own site ( http://www.binarynote.com ) score loads score is 99/100 in getmetrix.com
于 2014-06-30T04:00:22.333 回答
0

We have Just developed a code in C++ using that we can compress any code. The main function is given here for your ready reference.

//function to compress text files
void compress(char source[], char dest[])
{
  ifstream fin(source);
  ofstream fout(dest);
  char ch;
  int sp=0;
  while(fin.get(ch))
   {
      if(ch==' '||ch=='\t')
         sp++;
      else
         sp=0;
      if(ch=='\n' || ch=='\r')
         fout<<' ';
      else
          if(sp>=1)
             fout<<' ';
          else
             fout<<ch;
  } 
  fin.close();
  fout.close();
}
于 2016-10-25T15:35:55.273 回答
-2

Put this code to function.php:

class WP_HTML_Compression
{
  // Settings
  protected $compress_css = true;
  protected $compress_js = true;
  protected $info_comment = true;
  protected $remove_comments = true;
  // Variables
  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 compressed, size saved '.$savings.'%. From '.$raw.' bytes, now '.$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;
    // Variable reused for output
    $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;
          // Don't print the comment
          continue;
        }
        else if ($this->remove_comments)
        {
          if (!$overriding && $raw_tag != 'textarea')
          {
            // Remove any HTML comments, except MSIE conditional comments
            $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;
            // Remove any empty attributes, except:
            // action, alt, content, src
            $content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
            // Remove any space before the end of self-closing XHTML tags
            // JavaScript excluded
            $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');

on https://photogrist.com it's works fine :)

于 2015-04-29T13:12:07.747 回答