How to Compress PHP output in my plugin?

I never really dug into the compression options I have for my plugins and themes, so here I am. I was reading a few articles about PHP compression like this one and I had a question.

Is it really as easy as adding the following code in the appropriate spot?

<?php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
    ob_start("ob_gzhandler");
else
    ob_start();
?>

I definitely want to learn more about compression as we all know the benefits of it, but I am unsure exactly where to start. Any suggestions?

Are there better ways to effectively reduce the load time for my plugins and themes? It’s not that I’m having a problem with a particular piece of code, I just would like to expand my knowledge on the subject.

I am speaking in terms of a plugin or theme that would be used by many people, so this compression process would (hopefully) be sort of ‘automatic’, without having the user mess with their server configuration.

Solutions Collecting From Web of "How to Compress PHP output in my plugin?"

To answer your question, yes, it is that easy to enable compression. However, that’s only a small step when configuring a site for performance.

You should not attempt to handle compression from your plugin, unless the entire purpose of the plugin is load time optimization. Leave that to dedicated plugins, such as the WP Super-Cache or W3 Total Cache.

Try not to overcompensate because your site seems a bit sluggish.

  1. Run a performance test to figure out what issues your site really has
  2. Enabling GZIP in your webserver is probably a good idea
  3. Install a plugin such as WP Super Cache that handles caching, minifying and file script aggregation.
  4. Minimise the number of images each site contains, and make sure that small thumbnails are available (WP does that for you if you include images via the Add Media dialog).
  5. If you still have problems, try to disable suspicious plugins one by one. Many are badly written.