When viewing the source of your WordPress website, you may notice that there are a lot of HTML comments sitting there for no reason. HTML comments are not visible on the actual page and in some cases are there to help developers but are no longer needed within the code. A good plugin to use to help cleanup your site’s coding and remove any HTML comments is Head Cleaner, this plugin also gives you the option to remove any unwanted meta tags, javascript and css.
But if you don’t want to go down the plugin route, and just want a simple fix to put into your functions.php file, here it is…
function callback($buffer) { $buffer = preg_replace('/<!--(.|s)*?-->/', '', $buffer); return $buffer; } function buffer_start() { ob_start("callback"); } function buffer_end() { ob_end_flush(); } add_action('get_header', 'buffer_start'); add_action('wp_footer', 'buffer_end');
Depending on the size of your site, how many plugins you have and your server setup, this can be a little memerory intensive and might raise your server’s load, in which case, you could use a
This kind of output buffering is more memory intensive and might raise your server’s load.