An elegant solution to clean up html comments from wordpress themes

Typically, all popular wordpress themes come with html comments to mark endings of div block. This is required for theme developers as it is much easier to see where particular block ends.

However, I do not like leaving code like that in production site. Although some claim that HTML comments have no effect in SEO, it might have effect in keyword discovery process. Additionally, they might have effect for various security plugins, that filter and block content containing bad words.  Third, I think it is bad to leave debug information in production sites.

The biggest problem of these is that WordPress theme frameworks like Hybrid uses these as well. So you will have to clean up frameworks source each time you upgrade your framework. That is not the best approach.

However, You can implement this by creating couple filters in your childs (or custom) theme  functions.php file:

function custom_callback($buffer) {

$buffer=preg_replace('/<!--(.*)-->/Ui',' ',$buffer);

return $buffer;

}

function custom_buffer_start() { ob_start("custom_callback"); }

function custom_buffer_end() { ob_end_flush(); }

if (is_user_logged_in() ) {

} else {

add_action('wp_head', 'custom_buffer_start');

add_action('wp_footer', 'custom_buffer_end');

}

This will clean the code from comments when user is not logged in (for search engines and visitors).

Related posts:

  1. Hacking scissors wordpress plugin: resize based on image ratio Recently, I had simple but interesting problem on wordpress where I could not find ready automatic solution: thumbnail generation based on image ratio. I use...
  2. WordPress users hack – how to detect security breach in your blog user list Today was eventfull day. While launching a new version of 2-viruses.com I found a security breach, affecting WP 2.x, at least versions 2.0 and 2.1....
  3. Leaving unmoderated comments? Think again Comments are the basic social feature that most of the sites provide. A comment allows interaction between visitor and owner of website. However, comments pose...
  4. WordPress 2.5 – what is new It looks like only couple of weeks passed from the time I updated to 2.3, and it was time to update to 2.5 version of...
  5. Feedburner WordPress plugins : take care while upgrading to 2.7 or 2.8 Yesterday I tweeked my blog’s feeds and looked for a plugin to integrate feedburner seemlesly. This appeared a bit more tricky than thought: Upgrade to...


Leave a Reply