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).

Categories: Programming

Giedrius Majauskas

I am a internet company owner and project manager living at Lithuania. I am interested in computer security, health and technology topics.

2 Comments

Olaf Lederer · September 18, 2011 at 1:37 am

Nice solution but it doesn’t work in WP 3.21 anymore, should this work for comments which are created by plugins too?

    Giedrius Majauskas · September 18, 2011 at 2:44 am

    Yes, it should (explained bellow):
    1. You will see all the comments as admin. This is done for debugging reasons.
    2. wp_head used is launched just in the end of “head” section. This solution does not “catch” some of the comments in the header or in the very end of html. same goes for wp_footer
    The solution is to use different actions 🙂

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *