最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to remove html comment from source?

programmeradmin0浏览0评论

Is any easy method to remove all html comment like <!-- wp_head()--> from source from every pages and posts? Maybe using functions.php? Is any easy method without plugin?

Is any easy method to remove all html comment like <!-- wp_head()--> from source from every pages and posts? Maybe using functions.php? Is any easy method without plugin?

Share Improve this question edited Jan 24, 2018 at 16:39 Nathan Johnson 6,5286 gold badges30 silver badges49 bronze badges asked Jan 24, 2018 at 15:36 ნიკა ხაჩიძენიკა ხაჩიძე 891 gold badge3 silver badges7 bronze badges 1
  • I think Autoptimize does this, can you look up how they did it? – kero Commented Jan 24, 2018 at 15:37
Add a comment  | 

3 Answers 3

Reset to default 3

Here's a quick way to remove all comments using output buffering and preg_replace. We hook in before WordPress starts outputting the HTML to start the buffer. Then we hook in after WordPress stops outputting the HTML to get the buffer and strip the HTML comment tags from the content.

This code would ideally be in it's own plugin. But it could be in your functions.php if you're really adverse to adding more plugins.

namespace StackExchange\WordPress;

//* Start output buffering
function startOutputBuffer() {
  ob_start();
}

//* Print the output buffer
function stopOutputBuffer() {
  $html = ob_get_clean();
  echo strip_html_comments( $html );
}

//* See note for attribution of this code
function strip_html_comments( string $html ) : string {
  return preg_replace('/<!--(.*)-->/Uis', '', $html);
}

//* Add action before WordPress starts outputting content to start buffer
add_action( 'wp', __NAMESPACE__ . '\startOutputBuffer' );

//* Add action after WordPress stops outputting content to stop buffer
add_action( 'shutdown', __NAMESPACE__ . '\stopOutputBuffer' );

Note: Regex from this StackOverflow answer by Benoit Villière.

The following WordPress actions are able to filter your code from all HTML comments. Place the PHP code into your functions.php file inside the WordPress theme directory.

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');

The 2 above mentioned solutions didn't work for me (the first one caused a fatal website error, the second one had no effect), but I managed to do a mixture of both "solutions" that seems to work very well (when inserted into the functions.php of Wordpress):

function callback($buffer) {
    $buffer = preg_replace('/<!--(.*)-->/Uis', '', $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');

The source code looks much cleaner, "lighter" and more private then without potentially many html comments not meant for the public.

Keep in mind that it also cleaned "if" conditional comments from the public source code, but in my case they were mainly meant for the already very old Internet Explorer. I guess well done plugins etc. shouldn't work with conditionals formatted as html comments, right?

Any confirming or correcting opinion appreciated!

发布评论

评论列表(0)

  1. 暂无评论